2015/10/19

Android 自訂ListView 加入Button

如果用RelativeLayout的朋友無法觸發點擊事件請在RelativeLayout的屬性加入
android:descendantFocusability="blocksDescendants"

Button的屬性則要加入
android:focusable="false"


mylistView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/buttonOption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.01"
            android:text="Button" />

        <TextView
            android:id="@+id/textViewHelp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>





MyAdapter.java:
package com.cyfang.npust.myclass;

import java.util.ArrayList;
import java.util.HashMap;

import com.cyfang.npust.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MyAdapter extends BaseAdapter implements ListAdapter {
 private String[] TAG = new String[] { "Option", "Help" };
 private String[] OptionNames = new String[] { "1", "2" };
 private String[] HelpNames = new String[] { "A", "B" };

 private ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
 private Context context;

 public MyAdapter(Context context) {
  this.context = context;
  for (int index = 0; index < 2; index++) {
   HashMap<String, Object> hashMap = new HashMap<String, Object>();
   hashMap.put(TAG[0], OptionNames[index]);
   hashMap.put(TAG[1], HelpNames[index]);
   arrayList.add(hashMap);
  }
 }

 @Override
 public int getCount() {
  return arrayList.size();
 }

 @Override
 public Object getItem(int position) {
  return arrayList.get(position);
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

 @Override
 public View getView(int position, View view, ViewGroup group) {

  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View myView = inflater.inflate(R.layout.mylsitview, null);

  Button buttonOption = (Button) myView.findViewById(R.id.buttonOption);
  buttonOption.setText(arrayList.get(position).get(TAG[0]).toString());

  buttonOption.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    Toast.makeText(context, "Hi", 1).show();
   }
  });

  TextView textView = (TextView) myView.findViewById(R.id.textViewHelp);
  textView.setText(arrayList.get(position).get(TAG[1]).toString());

  return myView;
 }

}



Main.java:
package com.cyfang.npust;

import com.cyfang.npust.myclass.MyAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {
 private ListView listView;
 private Button buttonEngineer;
 private MyAdapter adpater;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_option);
  listView = (ListView) findViewById(R.id.listView);
  adpater = new MyAdapter(OptionActivity.this);
  listView.setAdapter(adpater);
 }

}


執行結果: