隐藏并显示视图上的视图单击android BaseAdapter中的listview问题

时间:2018-07-21 10:18:24

标签: android listview onclicklistener baseadapter

我试图在BaseAdapter的Listview的列表项的文本视图上显示隐藏的TextView。单击其他textviw可以看到listitem的Textview,但是列表的所有其他隐藏textview也是可见的,而不是listview的特定项。我想使特定的列表项视图可见,而不是每个列表项的所有textview可见。

下面是我用于列表项的XML文件。

listitem.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">
        <TextView
            android:id="@+id/tvShowView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Textview to Click"          
            />
        <TextView
            android:id="@+id/tvToGetVisiable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This Textview will get visiable on tvShowView click"
            android:visibility="gone"
            />
</LinearLayout>

以下是我的适配器类:-

RewardsAdapter.java

public class RewardsAdapter extends BaseAdapter {

    private List<Rewards> liRewards;
    private Context mContext;  

    public RewardsAdapter(Context context, List<Rewards> liRewards) {
        mContext = context;
        liRewards = liRewards;      
    }

    @Override
    public int getCount() {
        return (liRewards == null || liRewards.isEmpty()) ? 0 : liRewards.size();
    }

    @Override
    public Object getItem(int position) {
        return liRewards;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.listitem, null);
            viewHolder.tvShowView = (TextView) convertView.findViewById(R.id.tvShowView);
            viewHolder.tvToGetVisiable = (TextView)convertView.findViewById(R.id.tvToGetVisiable);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final Rewards rewards = liRewards.get(position);

        viewHolder.tvShowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewHolder.tvToGetVisiable.setVisibility(View.VISIBLE);
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private TextView tvShowView;
        private TextView tvToGetVisiable;
    }
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

使用此.....

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.listitem, null);
            viewHolder.tvShowView = (TextView) convertView.findViewById(R.id.tvShowView);
            viewHolder.tvToGetVisiable = (TextView)convertView.findViewById(R.id.tvToGetVisiable);

          //  convertView.setTag(viewHolder);

        final Rewards rewards = liRewards.get(position);

        viewHolder.tvShowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewHolder.tvToGetVisiable.setVisibility(View.VISIBLE);
            }
        });

        return convertView;
    }

希望复制问题将得到解决....

答案 1 :(得分:0)

您必须保存文本视图的可见性状态。保存1.00000并重用现有的ViewHolder时。 在ViewHolder方法中

getView