自定义Listview与按钮作为项OnItemClickListener wi

时间:2014-03-07 11:51:24

标签: android listview onitemclicklistener

我想使用按钮作为项目制作自定义ListView。现在我正在使用OnItemClickListener并且每次点击都会出现一个Toast消息,但是只有按下按钮,但是在ListView中出现(见图片)

enter image description here

如果我按下按钮而没有按下按钮,我怎么能这样做,吐司来了。

适配器代码:

public class MainListAdapter extends ArrayAdapter<Games> {

Context context;
int layoutResourceId;
Games data[] = null;

public MainListAdapter(Context context, int layoutResourceId, Games[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View row = convertView;
    ListHolder holder = null;

    if(row == null){
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ListHolder();
        holder.btn = (Button)row.findViewById(R.id.listViewButton);

        row.setTag(holder);
    }else{
        holder = (ListHolder)row.getTag();
    }
    Games games = data[position];
    holder.btn.setText(games.name);

    return row;
}

static class ListHolder {
    Button btn;
}

}

在Acitivty:

MainListAdapter adapter = new MainListAdapter(this, R.layout.listview_item_row, games_data);

    listView1 = (ListView)findViewById(R.id.listView1);

    final View header = (View)getLayoutInflater().inflate(R.layout.listview_newgame_row, null);
    listView1.addHeaderView(header);

    listView1.setAdapter(adapter);

    listView1.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show();
        }

    });

listview_item_row.xml:

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

<Button
    android:id="@+id/listViewButton"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:focusable="false"
    android:background="@drawable/button" />

3 个答案:

答案 0 :(得分:0)

在你的Adapter类中,为按钮创建两个对象。

用于adupter视图中按钮的setonclicklistener

E.g。

        vi = inflater.inflate(R.layout.list_row_contact, null);


    LinearLayout ll_adpt_main =   (LinearLayout)vi.findViewById(R.id.ll_adpt_main);
    TextView name = (TextView) vi.findViewById(R.id.name);
    TextView discription = (TextView) vi.findViewById(R.id.number);
    ImageView iv = (ImageView)vi.findViewById(R.id.iv);





    iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

        }
    });





    return vi;
}

答案 1 :(得分:0)

为按钮创建单独的onclicklistener。 OnItemclicklistener用于列表项。

答案 2 :(得分:0)

首先,你必须像这样创建一个xml布局。

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

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

然后创建一个适配器类:

class MyAdapter extends ArrayAdapter<View.OnClickListener> {
    public MyAdapter(Context context, int textViewResourceId, View.OnClickListener[] objects) {
        super(context, textViewResourceId, objects);
    }
    public MyAdapter(Context context, int resource, int textViewResourceId, List<View.OnClickListener> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.event_layout, null);

        Button b = (Button) convertView.findViewById(R.id.btn);

        View.OnClickListener l = getItem(position);
        b.setOnClickListener(l);

        return convertView;
    }
}

活动的布局应如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LayoutActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".YourActivityName" >
        <Button
            android:id="@+id/btnHeader1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
        <Button
            android:id="@+id/btnHeader2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

       <ListView
           android:id="@+id/your_list"
           android:layout_width="match_parent"
           android:layout_height="wrap_content">
       </ListView>
</LinearLayout>

现在设置列表项:

ListView list = (ListView)findViewById(R.id.your_list);

View.OnClickListener[] listeners = ....;

MyAdapter adapter = new MyAdapter(context, R.layout.your_list_layout_adapter, listeners);
list.setAdapter(adapter);

我希望满足您的所有要求:)