单击时备用 CheckTextBox 变得可见

Alternate CheckTextBox becoming visible on click

我正在尝试使 ListView 复选框的特定行(select由用户编辑)变得可见。但问题是,我使用扩展 ArrayAdapter 的 class 在我的列表视图中动态添加了 10 行,当我 select 第一行然后连同我的第一行、第三行、第五行等等复选框变得可见。与第 2、4 等行类似。 我只希望我 select 编辑的那个特定行(例如位置 0)显示复选框,其余的应该是不可见的。

public class LazyAdapter extends ArrayAdapter<RowItem> {

Context context;

public LazyAdapter(Context context, int resourceId, List<RowItem> items){
    super(context, resourceId, items);
    this.context = context;
}

public class ViewHolder{
    CheckedTextView checkbox;
    TextView title;
    TextView description;
    LinearLayout card;
}


public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder;
    RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null){
        convertView = mInflater.inflate(R.layout.list_row, null);
        holder = new ViewHolder();
        holder.card = (LinearLayout) convertView.findViewById(R.id.card);
        holder.checkbox = (CheckedTextView)convertView.findViewById(R.id.deliverychecktext);
        holder.title = (TextView)convertView.findViewById(R.id.title);
        holder.description = (TextView)convertView.findViewById(R.id.description);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder)convertView.getTag();

    //holder.image.setImageResource(rowItem.getImageId());
    holder.title.setText(rowItem.getTitle());
    holder.description.setText(rowItem.getDesc());

    Animation animation = AnimationUtils.loadAnimation(context, R.anim.card_animation);
    holder.card.startAnimation(animation);


    return convertView;
}
}

主要活动class

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

    // Intialize and set the Action Bar to Holo Blue
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33b5e5" )));

    ListView lv = (ListView) findViewById(R.id.myList);
     rowItems = new ArrayList<RowItem>();

        String[] titles = {"Address1","Address2","Address3","Address4","Address5","Address6","Address7","Address8"};
        String[] descriptions = {"First Address","Second Address","Third Address","Fourth Address","Fifth Address",
                "Sixth Address","Seventh Address","Eighth Address"};
        //Populate the List
        for (int i = 0; i < titles.length; i++) {
            RowItem item = new RowItem("Delivery Address :", descriptions[i]);
            rowItems.add(item);
        }

        // Set the adapter on the ListView
        LazyAdapter adapter = new LazyAdapter(getApplicationContext(), R.layout.list_row, rowItems);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                CheckedTextView check = (CheckedTextView) view.findViewById(R.id.deliverychecktext);                                
                if (check.getVisibility() == 4)
                check.setVisibility(1);
                else
                    check.setVisibility(4);
                Toast.makeText(getApplicationContext(), "h u clicked : "+position, 
                           Toast.LENGTH_LONG).show();

            } 


            });

list_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/card_no_border"
android:orientation="vertical"
android:padding="2dp" 
android:id="@+id/card">

<CheckedTextView
android:id="@+id/deliverychecktext"
 android:layout_width="fill_parent"
 android:paddingLeft="2dp"
 android:paddingRight="2dp"
 android:paddingTop="2dp"
 android:layout_height="wrap_content"
 android:checkMark="@drawable/ic_launcher"
 android:visibility="invisible"
/>

   <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:text="Dog Tag"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#040404"
    android:textStyle="bold"
    android:typeface="sans" />
</LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#e5e5e5">

<TextView android:layout_width="match_parent"
      android:layout_height="wrap_content" 
      android:text="Your Saved Address"
      android:textSize="20sp"
      android:padding="5dp"/>

<ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:divider="@android:color/transparent"
        android:dividerHeight="10dp"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="1dip"/>
</LinearLayout>

第一个 RowItem 作为 class 变量来存储所选项目。

private RowItem mSelectedItem = null;

例如

public class LazyAdapter extends ArrayAdapter<RowItem> {
Context context;
private RowItem mSelectedItem = null;

之后将以下侦听器添加到您的 LazyAdapter 适配器 class。

/**
 * On Click Listener for the view.
 */
protected class OnViewClickListener implements
        android.view.View.OnClickListener {

    private RowItem mItem;

    public OnViewClickListener(RowItem item) {
        mItem = item;
    }

    @Override
    public void onClick(View v) {
        mSelectedItem = mItem;

        notifyDataSetChanged();
    }
}

在 return 查看之前从 LazyAdapter 中的 getView 方法调用此侦听器,并为显示和隐藏视图添加逻辑。

例如:

    if(mSelectedItem != null && mSelectedItem == rowItem) {
        holder.checkbox.setVisibility(View.VISIBLE);
    } else {
        holder.checkbox.setVisibility(View.INVISIBLE);
    }

    convertView.setOnClickListener(new OnViewClickListener(rowItem));

    return convertView;
}

并删除您 activity 中的点击列表器。

在你的 activity 它的工作是因为在渲染适配器视图时它会重用对象,所以它就是这样发生的。现在我们在视图渲染时每次都在检查和更新。