多选ListView带复选框和MarkAll按钮,如联系人app(复选框仅在选择模式下显示)

时间:2016-11-08 07:00:34

标签: android listview multichoiceitems

我需要一个ListView,可以通过它们附近的复选框选择哪些项目,并且复选框不显示在默认模式下但是它们必须以选择模式显示(当触摸其中一个项目时)和操作栏中的MarkAll按钮< strong>与默认联系人应用程序完全相同(见下图)。

enter image description here 我找到了一些东西,我找到了联系人app surce代码,但它太复杂我只需要两个功能的联系人应用程序,在其他例子中,我发现在listview中显示复选框总是我的意思不仅在选择模式下,或仅在CHOICE_MODE_MULTIPLE_MODAL上设置列表视图的选择模式,而且根本没有复选框。

我也尝试自己创建列表视图CHOICE_MODE_MULTIPLE_MODAL并编写适当的事件,并在CheckView项目xml文件中放入CheckedTextView。

这是我的ListView项目xml布局

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

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="200dp"
    android:layout_height="30dp"
    android:layout_alignParentRight="true"
    android:text="Study cursors"
     android:ellipsize="end"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/tvTags_Album"
    android:layout_width="200dp"
    android:layout_height="25dp"
    android:layout_alignParentRight="true"
    android:layout_below="@id/tvTitle"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:text=""
    android:ellipsize="end"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/imgPic_Grid"
    android:layout_width="120dp"
    android:layout_height="90dp" />

<TextView
    android:id="@+id/tvModifiedDate"
    android:layout_width="200dp"
    android:layout_height="25dp"
    android:layout_alignBottom="@+id/imgPic_Grid"
    android:layout_alignParentRight="true"
    android:text="تاریخ ایجاد : "
    android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckedTextView 
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
 />
 </RelativeLayout>

这是我的适配器代码

public class MediaListAdaptor extends CursorAdapter {
   public ImageLoader imageLoader;
  public MediaListAdaptor(Context context, Cursor cursor) {
      super(context, cursor, 0);
  }

  // The newView method is used to inflate a new view and return it, 
  // you don't bind any data to the view at this point. 
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      return LayoutInflater.from(context).inflate(R.layout.activity_medialist_item, parent, false);
  }
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
      // Find fields to populate in inflated template
      if (cursor != null)
      {

          TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
          TextView tvtags = (TextView) view.findViewById(R.id.tvTags_Album);
          TextView tvModifiedDate = (TextView) view.findViewById(R.id.tvModifiedDate);
          ImageView imgPic = (ImageView) view.findViewById(R.id.imgPic_Grid);

          // Extract properties from cursor
          String FileName = cursor.getString(cursor.getColumnIndexOrThrow("FileName"));
          String Title = cursor.getString(cursor.getColumnIndexOrThrow("Title"));
          String Tags = cursor.getString(cursor.getColumnIndexOrThrow("Tags")).replace("\n", "").replace("\r", "");
          String ModifiedDate = cursor.getString(cursor.getColumnIndexOrThrow("LastModifiedDate"));
        /*  final String path = Environment.getExternalStorageDirectory().getPath()
                +"/"+  Environment.DIRECTORY_DCIM+"/Camera/2.jpg";*/
          final String path = cursor.getString(cursor.getColumnIndex("filepath"));
          if(path != "")
          {
              File imgFile = new  File(path);

                if(imgFile.exists()){
                     imageLoader=new ImageLoader(context);
                     imageLoader.DisplayImage(path, imgPic);

                }
          }

          tvTitle.setText(String.valueOf(Title));
          tvtags.setText(String.valueOf(Tags));
          tvModifiedDate.setText("تاریخ ایجاد : "+ModifiedDate);
      }


  }
  }

这是我的主要活动代码的一部分,它负责绑定ListView并将其设置为多选和相关事件

 Cursor cr = mRepository.GetMediaForAlbum(AlbumId);
      BindListViewAndGrid(cr);

         lvItems.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
         lvItems.setMultiChoiceModeListener(new MultiChoiceModeListener() {

            @Override
            public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
                MenuInflater inflater =arg0.getMenuInflater(); 
                inflater.inflate(R.menu.multiselect_remove, arg1);
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {

                return false;
            }

            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                IDList.add(id);
                Toast.makeText(MainActivity.this,Long.toString(id)+"< >"+Integer.toString(position) , Toast.LENGTH_LONG).show();
                .0
            }
        });

如上所示,我在List View项目中使用了CheckedTextView,但我遇到了两个主要问题,首先显示CheckedTextView,不仅仅是在选择模式下(我希望CheckedTextView仅在选择模式下出现)< / strong>和第二个CheckedTextView既不能在默认模式下也不能在选择模式下检查

你知道任何具有上述功能的示例应用程序或任何修复我自己的代码的解决方案吗?

提前致谢

0 个答案:

没有答案