代码之家  ›  专栏  ›  技术社区  ›  Stanislav Batura

如何在ListView项的视图上添加不同的OnClickListeners来执行不同的操作

  •  0
  • Stanislav Batura  · 技术社区  · 6 年前

    ListView 项目被单击?
    换句话说,当你点击不同的视图时 项目应用应执行其他操作。
    在细节上,我有一个简单的 Book.java 类,该类包含一些书籍描述。
    ListView<Book> 使用 BooksAdapter.class :

    public class BooksAdapter extends ArrayAdapter<Book> {
        public BooksAdapter (Context context, List<Book> books) {
            super(context,0,books);
        }
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    
    //        ConstraintLayout constraintLayout = new ConstraintLayout();
    //        constraintLayout.setVisibility();
            View listItemView = convertView;
            if (listItemView == null) {
                listItemView = LayoutInflater.from(getContext()).inflate(
                        R.layout.books_list_item, parent, false);
            }
            Book currentBook = getItem(position);
            ImageView coverView = (ImageView) listItemView.findViewById(R.id.preview_image_view);
            if (currentBook.getImage() == "No cover") {
                coverView.setImageResource(R.drawable.no_book_cover);
            } else {
                Picasso.get().load(currentBook.getImage()).into(coverView);
            }
            TextView authorTextView = (TextView)listItemView.findViewById(R.id.autor_text);
            authorTextView.setText(formatAuthor(currentBook.getAuthor(),currentBook.getDate()));
            TextView titleTextView = (TextView)listItemView.findViewById(R.id.title_text);
            titleTextView.setText(currentBook.getTitle());
            //TextView descrTextView = (TextView)listItemView.findViewById(R.id.description_text);
            //descrTextView.setText(currentBook.getDescription());
            return listItemView;
        }
    
        private String formatAuthor (String name,String date ) {
            name = name.substring(2,name.length()-2);
            date = date.substring(0,4);
            String fullString = name + ", " + date;
            return(fullString);
        }
    }  
    

    books_list_item.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/list_item"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:orientation="horizontal"
            android:paddingEnd="16dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingStart="16dp"
            android:paddingBottom="16dp"
            android:paddingTop="8dp">
            <ImageView
                android:id="@+id/preview_image_view"
                android:layout_width="80dp"
                android:layout_height="match_parent" />
            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/autor_text"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:maxLines="1"
                    android:paddingLeft="8dp"
                    android:paddingRight="8dp"
                    android:paddingTop="8dp"
                    android:textColor="@color/textColorPrimary"
                    android:textSize="16sp" />
                <TextView
                    android:id="@+id/title_text"
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:layout_gravity="center_vertical"
                    android:maxLines="2"
                    android:paddingLeft="8dp"
                    android:paddingRight="8dp"
                    android:paddingTop="0dp"
                    android:textColor="@color/textColorLight"
                    android:textSize="16sp" />
            </LinearLayout>
            <ImageView
                android:layout_width="40dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_info_black_24dp"
                android:scaleType="center"/>
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@android:color/darker_gray"/>
    </LinearLayout>
    

    MainActivity.java 创建mAdapter并覆盖 setOnItemClickListener :

       ListView booksListView = (ListView) findViewById(R.id.list);
        mAdapter = new BooksAdapter(this, new ArrayList<Book>());
        booksListView.setAdapter(mAdapter);
    
        booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Book book = mAdapter.getItem(position);
    
                ad = new AlertDialog.Builder(BooksListActivity.this);
                ad.setTitle("Book description");
                ad.setMessage(book.getDescription());
    
                AlertDialog alert = ad.create();
                alert.show();
                //ad.create();
            }
        }); 
    

    下面是一些一般性的问题 onClickListeners 不一样的 Views @+id/autor_text @+id/title_text 例如)并在这些情况下执行不同的操作?

    2 回复  |  直到 6 年前
        1
  •  4
  •   Abhinav Suman    6 年前

    在你的内心 getView() 方法集 onClickListener 完全不同 Views

    通过实现此代码:-

    authorTextView.setOnClickListener(this);
    titleTextView.setOnClickListener(this);  
    
    public void onClick(View v) {
    
          switch(v.getId()){  
               case R.id.authorTextView:  
                    //code to be written to handle the click event
               break;
    
               case R.id.titleTextView: 
                    //code to be written to handle the click event 
               break;
          }
        }
    };
    
        2
  •  0
  •   prashant17 OzanTabakoglu    6 年前

    在你的内心 getView

        authorTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Do your action
            }
        });
    
    
        titleTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Do your action
            }
        });