如何在按钮上隐藏/显示Imageview单击自定义适配器布局

时间:2017-05-21 07:23:25

标签: android listview

我有ListView的自定义适配器。 ListView位于activity_main.xml,我ImageView布局CustomAdapterListView使用CustomAdapter填充。{/ p>

我的问题是我必须使用主布局中的按钮隐藏/显示自定义布局中的图像。就像我的ListView中有10行,10行有该图像一样。我想只隐藏特定的行。

我希望你理解我的问题......先谢谢你

这是我的ListViewAdapter

public class ListViewAdapter extends ArrayAdapter<FileName> {
    Context context;
    LayoutInflater inflater;
    List<FileName> filenames;
    private SparseBooleanArray mSelectedItemsIds;

    public ListViewAdapter(Context context,int resource,List<FileName> filenames) {
        super(context, resource, filenames);
        mSelectedItemsIds = new SparseBooleanArray();
        this.context = context;
        this.filenames = filenames;
        inflater = LayoutInflater.from(context);
    }

    private class ViewHolder {
        TextView filename;
        TextView shorttext;
        ImageView imageView;
    }

    public View getView(int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.listview_item, null);
            // Locate the TextViews in listview_item.xml
            holder.filename = (TextView) view.findViewById(R.id.item_title);
            holder.shorttext = (TextView) view.findViewById(R.id.item_desc);
            holder.imageView = (ImageView) view.findViewById(R.id.lock);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Capture position and set to the TextViews<br />
        holder.filename.setText(filenames.get(position).getName());
        holder.shorttext.setText(filenames.get(position).getShorttext());

        return view;
    }
}

name = filenames.get(position).getName(); //从listview获取positin    filenames.set(name,FileName.isVisible = true); //在isVisible上获得错误

3 个答案:

答案 0 :(得分:1)

我已编写示例代码并经过测试

<强> MainActivity

public class MainActivity extends AppCompatActivity {

List<FileName> filenames;
DBhelper dBhelper;dBhelper
SQLiteDatabase sqLiteDatabase;
ListViewAdapter listViewAdapter;
ListView listView;
ImageView lock;
String name;

//temporary button i have added
Button lockBTN;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    listView = (ListView) findViewById(R.id.lv_filename);
    lockBTN = (Button) findViewById(R.id.lock);

    filenames = getResult();
    listViewAdapter = new ListViewAdapter(this, filenames);
    listView.setAdapter(listViewAdapter);

    lockBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //giving statically position is 2
            int position = 2;

            // when you press menu button or any other button
            FileName fileName = filenames.get(position);
            //here you can set true-Show and false-hide imageview
            fileName.setVisible(true);

            //and update the list
            filenames.set(position, fileName);
            listViewAdapter.notifyDataSetChanged();
        }
    });
}

//temporary data to show on listview
private List<FileName> getResult() {
    List<FileName> fileNames = new ArrayList<>();
    String[] nameArr = {"Name1", "Name2", "Name3"};
    String[] shortTextArr = {"ShortText1", "ShortText2", "ShortText3"};

    for(int i=0; i<nameArr.length;i ++){
        FileName fileName = new FileName();
        fileName.setName(nameArr[i]);
        fileName.setShorttext(shortTextArr[i]);
        //setting default is hide to imageview
        fileName.setVisible(false);
        fileNames.add(fileName);
    }
    return fileNames;
}
}

<强>文件名

public class FileName {
private String name;
private String shorttext;
private boolean visible = true;


public String getName() {
    return name;
}

public void setName(String _name) {
    this.name = _name;
}

public String getShorttext() {
    return shorttext;
}

public void setShorttext(String _shorttext) {
    this.shorttext = _shorttext;
}

public boolean isVisible() {
    return visible;
}

public void setVisible(boolean visible) {
    this.visible = visible;
}
}

<强> ListViewAdapter

public class ListViewAdapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
List<FileName> filenames;
private SparseBooleanArray mSelectedItemsIds;

public ListViewAdapter(Context context, List<FileName> filenames) {
    mSelectedItemsIds = new SparseBooleanArray();
    this.context = context;
    this.filenames = filenames;
    inflater = LayoutInflater.from(context);
}

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

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

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


public View getView(int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.listview_item, null);

        // Locate the TextViews in listview_item.xml
        holder.filename = (TextView) view.findViewById(R.id.item_title);
        holder.shorttext = (TextView) view.findViewById(R.id.item_desc);
        holder.imageView = (ImageView) view.findViewById(R.id.lock);

        //holder.filename.setPaintFlags(holder.filename.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    // Capture position and set to the TextViews<br />
    holder.filename.setText(filenames.get(position).getName());
    holder.shorttext.setText(filenames.get(position).getShorttext());

    // check weather image is to show/hide
    if (filenames.get(position).isVisible()) {
        holder.imageView.setVisibility(View.VISIBLE);
    } else {
        holder.imageView.setVisibility(View.GONE);
    }
    return view;
}

private class ViewHolder {
    TextView filename;
    TextView shorttext;
    ImageView imageView;
}
}

<强> activity_main.xml中

<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"
android:padding="5dp"
android:orientation="vertical">

<ListView
    android:id="@+id/lv_filename"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/lock"/>

<Button
    android:id="@+id/lock"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Lock"/>

</RelativeLayout>

<强> custom_list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/utilities_notepad_icon" />
<LinearLayout
    android:layout_width="232dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="0.77">

    <TextView
        android:id="@+id/item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="1dp"
        android:paddingTop="5dp"
        android:text="Item Title"
        android:textSize="16dp" />

    <TextView
        android:id="@+id/item_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLength="35"
        android:paddingBottom="5dp"
        android:paddingTop="1dp"
        android:text="Item Description"
        android:textColor="#999"
        android:textSize="10dp" />
</LinearLayout>

<ImageView
    android:id="@+id/lock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/lo"
    android:layout_marginTop="13sp" />
</LinearLayout>

答案 1 :(得分:0)

FileName课程中加一个额外的变量。

public class FileName {
    public String name;
    public String shortText; 
    public boolean visible = true; // Set the default value to true. 
}

现在,在主布局中单击外部按钮,您可以考虑相应地更新列表项。只需将visible变量设置为您要隐藏的项目的false即可。然后在适配器上调用notifyDataSetChanged()以重新加载数据。

是的,在您的适配器中,只需检查visible属性的值,然后决定隐藏/显示ImageView。因此适配器的getView功能应该如下所示。

public View getView(int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.listview_item, null);

        // Locate the TextViews in listview_item.xml
        holder.filename = (TextView) view.findViewById(R.id.item_title);
        holder.shorttext = (TextView) view.findViewById(R.id.item_desc);
        holder.imageView = (ImageView) view.findViewById(R.id.lock);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    // Capture position and set to the TextViews<br />
    holder.filename.setText(filenames.get(position).getName());
    holder.shorttext.setText(filenames.get(position).getShorttext());

    // Hide/Show the ImageView
    if (filenames.get(position).visible) holder.imageView.setVisibility(View.VISIBLE);
    else holder.imageView.setVisibility(View.GONE);

    return view;
}

答案 2 :(得分:0)

我建议您使用RecyclerView而不是ListView。 RecyclerView比ListView获得更多性能。现在所有人都使用RecyclerView而不是旧的ListView,它也很容易使用。 为了处理你的案例,创建一个函数来删除你想要在RecyclerViewAdapter类中删除的文件名,如下所示

removeImageView(position){
  fileNames.remove(position);
  this.notifyItemRemoved(position);
}

notifyItemRemoved调用将刷新回收器视图中的位置,您还可以获得一个很好的视图,可以立即删除动画。