Listview Onclick Listener更新Cardview Layout

时间:2016-12-24 21:32:01

标签: java android xml android-layout

我更新了布局,用Buttons instaed替换了ImageViews,当我使用带有ImageViews的旧布局时,Listview OnCLick监听器现在不能正常工作。如果需要进一步格式化,请告诉我,任何帮助都将被解决。

活动中的监听器:

lvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(MainActivity.this, ItemDetailsActivity.class);
        intent.putExtra("Position", position);
        startActivity(intent);
    }
});

list_item.xml:

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:id="@+id/ItemNametv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Item : "
                android:textSize="20sp"
                android:layout_marginBottom="8dp"/>

            <TextView
                android:id="@+id/qtytv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Qty : "
                android:layout_marginBottom="8dp"/>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#c0c0c0"
                android:layout_marginBottom="8dp"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/EditItem"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Edit"
                    android:minWidth="0dp"
                    android:minHeight="0dp"
                    style="@style/Widget.AppCompat.Button.Borderless"/>

                <Button
                    android:id="@+id/DeleteItem"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Delete"
                    android:textColor="@color/btn_logut_bg"
                    android:minWidth="0dp"
                    android:minHeight="0dp"
                    style="@style/Widget.AppCompat.Button.Borderless"/>

            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

ItemDetailsAdapter.java:

public class ItemDetailsAdapter extends BaseAdapter {
    private ArrayList<Item> arrayListItem;
    private Context context;
    private LayoutInflater inflater;

    public ItemDetailsAdapter(Context context, ArrayList<Item> arrayListItem) {
        this.context = context;
        this.arrayListItem = arrayListItem;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;
        Holder holder;
        if (v == null) {
            v = inflater.inflate(R.layout.list_item, null);
            holder = new Holder();
            holder.ItemName = (TextView) v.findViewById(R.id.ItemNametv);
            holder.qty = (TextView) v.findViewById(R.id.qtytv);
            holder.EditItem = (Button) v.findViewById(R.id.EditItem);
            holder.DeleteItem = (Button) v.findViewById(R.id.DeleteItem);
            v.setTag(holder);
        } 
        else {
            holder = (Holder) v.getTag();
        }

        holder.ItemName.setText(arrayListItem.get(position).getItem());
        holder.qty.setText(arrayListItem.get(position).getQty());
        holder.EditItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,AddOrUpdateItem.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("Position", position);
                context.getApplicationContext().startActivity(intent);
            }
        });
        holder.DeleteItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowConfirmDialog(context, position);
            }
        });
        return v;
    }

    class Holder {
        TextView ItemName, qty;
        Button DeleteItem, EditItem;
    }

    public static void ShowConfirmDialog(Context context, final int position) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder
            .setMessage("Are you sure you want to delete this entry?")
            .setCancelable(true)
            .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    MainActivity.getInstance().deleteItem(position);
                }
            })
            .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

3 个答案:

答案 0 :(得分:2)

存在观点冲突的问题,即观点可能会被其他人重叠,所以添加

您的父级布局和列表项上的

android:focusable="false"以及您要为其处理点击事件的所有视图,对于他们,mRecyclerView.computeHorizontalScrollOffset(); mRecyclerView.computeVerticalScrollOffset(); 在此处添加此按钮...

答案 1 :(得分:1)

尝试这个

data["eventType"] = ["*ABC"]

答案 2 :(得分:1)

  • 第1步:在 list_item.xml 中,将android:descendantFocusability="blocksDescendants"添加到您的根Linearlayout

  • 第2步:在 list_item.xml 中,将android: focusable="false"添加到android.support.v7.widget.CardView

注意:如果android:clickable="true"中有android.support.v7.widget.CardView则将其删除。