如何使用CustomAdapter使setOnItemClickListener适用于ListView?

时间:2018-01-27 15:41:58

标签: android listview custom-adapter onitemclicklistener android-viewholder

我有一个小项目来管理足球自由泳的落地技巧。

该应用程序就像是管理我们节目的TVShowTime应用程序。

着陆按钮正在工作,在项目列表中,请参阅下面的列表视图。

listview

一切正常但当我点击列表中的任何项目时都没有任何反应。

谢谢。

CustomAdapter

public class CustomAdapter extends ArrayAdapter  {

    private String[] tricks;
    private ImageView imageView;
    private Activity context;
    private boolean isClicked;

    public CustomAdapter(@NonNull Activity context, String[] tricks, ImageView unmastered) {
        super(context, R.layout.custom_listview_tricks, tricks);

        this.context = context;
        this.tricks = tricks;
        this.imageView = unmastered;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View r= convertView;

        viewHolder viewHolder = null;

        if(r==null){

            LayoutInflater layoutInflater = context.getLayoutInflater();
            r = layoutInflater.inflate(R.layout.custom_listview_tricks, null, true);
            viewHolder = new viewHolder(r);
            r.setTag(viewHolder);
        } else {

            viewHolder = (CustomAdapter.viewHolder) r.getTag();

        }

        viewHolder.textView1.setText(tricks[position]);

        viewHolder.unmastered.setBackgroundResource(R.drawable.unmastered);

        final CustomAdapter.viewHolder finalViewHolder = viewHolder;

        viewHolder.unmastered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if(isClicked){
                    finalViewHolder.unmastered.setBackgroundResource(R.drawable.mastered);
                }else{
                    finalViewHolder.unmastered.setBackgroundResource(R.drawable.unmastered);
                }
                isClicked = !isClicked;

            }
        });

viewHolder.textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(getContext(), "TEST", Toast.LENGTH_SHORT).show();

            }
        });

        return r;
    }


    class viewHolder{

        TextView textView1;
        ImageView unmastered;
        viewHolder (View v){

            textView1 = v.findViewById(R.id.name_xml);
            unmastered = v.findViewById(R.id.unmastered_xml);

        }

    }

}

TricksActivity

public class TricksActivity extends AppCompatActivity {

    private String[] lower = {

            "ATW - Around the World",
            "HTW - Hop the World",
            "Crossover",
            "Crossover 360",
            "Simple Crossover",
            "Reverse Crossover",
            "KATW - Knee Around the World",
            "KHTW - Knee Hop the World",
            "Toe Bounce",
            "Reverse Toe Bounce",
            "Air Jester",
            "ATL - Around the Leg",
            "Hell Juggles"

    };

    private ImageView imageView;


    private int codigo;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tricks);


        imageView = findViewById(R.id.unmastered_xml);
        //imageView.setBackgroundResource(R.drawable.mastered);


        Intent intent = getIntent();
        codigo = intent.getIntExtra("codigo", 0);

        //Toast.makeText(this, ""+codigo, Toast.LENGTH_SHORT).show();

        listView = findViewById(R.id.listview_tricks_xml);

        CustomAdapter customAdapter = new CustomAdapter(this, lower, imageView);

        listView.setAdapter(customAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

             Toast.makeText(TricksActivity.this, ""+position, Toast.LENGTH_SHORT).show();

            }

            }
        });

    }

}

activity_tricks.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dlapps.tricksmasterizadas.tricksmasterizadas.TricksActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="15dp">


        <ListView
            android:id="@+id/listview_tricks_xml"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:dividerHeight="1dp"
            >

        </ListView>

    </LinearLayout>




</RelativeLayout>

custom_listview_tricks.xml

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

    <TextView
        android:layout_width="310dp"
        android:layout_height="50dp"
        android:text="1  ATW - Around the World"
        android:id="@+id/name_xml"
        android:textStyle="bold"
        android:textSize="20dp"
        android:gravity="center"
        android:layout_marginTop="3dp"/>

    <ImageButton
        android:id="@+id/unmastered_xml"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="315dp"
        android:layout_marginTop="3dp"
        android:background="@drawable/unmastered" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

问题解决了

我刚刚在custom_list_view.xml中添加了这段代码,最后setOnClickListener工作了。

android:descendantFocusability="blocksDescendants"

现在我的布局是:

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