单击Listview Android上的项目时显示按钮

时间:2018-09-12 02:01:10

标签: android

我有一个这样的列表视图:

enter image description here

在列表视图的单行上,我有2个按钮,并将默认可见性设置为“消失”:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@mipmap/ic_launcher_round" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="List Item"
    android:textColor="#000"
    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/imageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/imageView"
    app:layout_constraintTop_toTopOf="@+id/imageView" />

<Button
    android:id="@+id/btn1"
    android:layout_width="171dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Go"
    android:visibility="gone"
    app:layout_constraintStart_toStartOf="@+id/imageView"
    app:layout_constraintTop_toBottomOf="@+id/imageView" />

<Button
    android:id="@+id/btn2"
    android:layout_width="167dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="Cancel"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/btn1"
    app:layout_constraintTop_toTopOf="@+id/btn1" />

我要设置的是,当我单击列表中的一个项目时,在被单击的行上将显示2按钮,而当我再次单击该行时,该按钮将被隐藏,与其他行相同。你能帮我吗?

这是我的MainActivity.class,这只是一个演示,所以我只在MainActivity中编写:

public class MainActivity extends AppCompatActivity {

private ListView mList;
private ArrayList<String> data = new ArrayList<String>();
Button button1, button2;


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

    mList = findViewById(R.id.listview);

    generateListContent();

    mList.setAdapter(new MyListAdaper(this, R.layout.list_item, data));

    mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "List item was clicked at " + position, Toast.LENGTH_SHORT).show();
        }
    });
}

private void generateListContent() {
    for(int i = 0; i < 55; i++) {
        data.add("This is row number " + i);
    }
}

private class MyListAdaper extends ArrayAdapter<String> {

    private int layout;
    private List<String> mObjects;

    private MyListAdaper(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
        mObjects = objects;
        layout = resource;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mainViewholder = null;
        if(convertView == null) {

            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(layout, parent, false);
            ViewHolder viewHolder = new ViewHolder();

            viewHolder.thumbnail = convertView.findViewById(R.id.imageView);
            viewHolder.title = convertView.findViewById(R.id.textView);
            viewHolder.button1 =  convertView.findViewById(R.id.btn1);
            viewHolder.button2 =  convertView.findViewById(R.id.btn2);

            convertView.setTag(viewHolder);
        }
        mainViewholder = (ViewHolder) convertView.getTag();
        mainViewholder.button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "Button was clicked for list item " + position, Toast.LENGTH_SHORT).show();
            }
        });
        mainViewholder.title.setText(getItem(position));

        return convertView;
    }
}
public class ViewHolder {
    ImageView thumbnail;
    TextView title;
    Button button1, button2;
}

3 个答案:

答案 0 :(得分:0)

首先设置Button visibility invisible而不是gone

将此代码添加到getView()中的Array Adapter函数中。

boolean clicked = false;
convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clicked = !clicked
                if (clicked) {
                     mainViewholder.button1.setVisibility(View.VISIBLE);
                     mainViewholder.button2.setVisibility(View.VISIBLE);
                } else {
                     mainViewholder.button1.setVisibility(View.INVISIBLE);
                     mainViewholder.button2.setVisibility(View.INVISIBLE);
                }
            }
        });

答案 1 :(得分:0)

首先创建新的xml资源文件(例如tag.xml)

<resources>
        <item name="BUTTON_SHOWING" type="id"/>
</resources>

然后只需为您的itemClickLister设置listview如下

mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            ViewHolder viewHolder = (ViewHolder) view.getTag();

            if(view.getTag(R.id.BUTTON_SHOWING)==null) {

                    viewholder.button1.setVisibility(View.VISIBLE);
                    viewholder.button2.setVisibility(View.VISIBLE); 
                    view.setTag(R.id.BUTTON_SHOWING,new Object());
                }
                else{
                     viewholder.button1.setVisibility(View.GONE);
                     viewholder.button2.setVisibility(View.GONE);
                    view.setTag(R.id.BUTTON_SHOWING,null);
                }

        }
    });

答案 2 :(得分:0)

您可以按以下方式修改代码

  public class MainActivity extends AppCompatActivity {

private ListView mList;

private ArrayList<ListModel> data = new ArrayList<>();


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

    mList = findViewById(R.id.listview);

    generateListContent();
    mList.setAdapter(new MyListAdaper(this, R.layout.list_item, data));


}

private void generateListContent() {
    for (int i = 0; i < 55; i++) {
        ListModel listModel = new ListModel();
        listModel.setMsg("This is row number " + i);
        data.add(listModel);
    }
}

private class MyListAdaper extends ArrayAdapter<ListModel> {

    private int layout;

    private List<ListModel> mObjects;

    private MyListAdaper(Context context, int resource, List<ListModel> objects) {
        super(context, resource, objects);
        mObjects = objects;
        layout = resource;
    }

    void toggleVisibility(int index) {
        mObjects.get(index).toggle();

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mainViewholder = null;
        if (convertView == null) {

            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(layout, parent, false);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.linearLayout = convertView.findViewById(R.id.container);
            viewHolder.thumbnail = convertView.findViewById(R.id.imageView);
            viewHolder.title = convertView.findViewById(R.id.textView);
            viewHolder.button1 = convertView.findViewById(R.id.btn1);
            viewHolder.button2 = convertView.findViewById(R.id.btn2);

            convertView.setTag(viewHolder);
        }
        mainViewholder = (ViewHolder) convertView.getTag();
        mainViewholder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "List item was clicked at " + position, Toast.LENGTH_SHORT).show();
                toggleVisibility(position);
                notifyDataSetChanged();
            }
        });
        mainViewholder.button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "Button was clicked for list item " + position, Toast.LENGTH_SHORT).show();
            }
        });
        ListModel listModel = getItem(position);
        mainViewholder.setData(listModel);
        //mainViewholder.title.setText();

        return convertView;
    }
}


public class ViewHolder {
    ImageView thumbnail;

    TextView title;

    Button button1, button2;

    LinearLayout linearLayout;

    void setData(ListModel data) {
        title.setText(data.getMsg());
        if (data.isShowBtn()) {
            button1.setVisibility(View.VISIBLE);
            button2.setVisibility(View.VISIBLE);
        } else {
            button1.setVisibility(View.INVISIBLE);
            button2.setVisibility(View.INVISIBLE);
        }
    }
}

// Model class to hold the data for the list view items.
public class ListModel {
    // Variable to hold the visibility of buttons. 
    private boolean showBtn;

    private String msg;

    // Function to toggle the visibility of buttons.
    public void toggle() {
        this.showBtn = !this.showBtn;
    }

    public boolean isShowBtn() {
        return showBtn;
    }

    public void setShowBtn(boolean showBtn) {
        this.showBtn = showBtn;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
}

您的xml文件将被修改为

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:id="@+id/container"
android:layout_height="match_parent">
<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@mipmap/ic_launcher_round" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="List Item"
    android:textColor="#000"
    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/imageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/imageView"
    app:layout_constraintTop_toTopOf="@+id/imageView" />

<Button
    android:id="@+id/btn1"
    android:layout_width="171dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Go"
    android:visibility="gone"
    app:layout_constraintStart_toStartOf="@+id/imageView"
    app:layout_constraintTop_toBottomOf="@+id/imageView" />

<Button
    android:id="@+id/btn2"
    android:layout_width="167dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="Cancel"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/btn1"
    app:layout_constraintTop_toTopOf="@+id/btn1" />
   </LinearLayout>