如何从 ScrollView 中的 ImageView 中选择项目?

Posted

技术标签:

【中文标题】如何从 ScrollView 中的 ImageView 中选择项目?【英文标题】:How to select item from ImageView in ScrollView? 【发布时间】:2020-10-19 17:06:34 【问题描述】:

由于我是 android studio 的新手,请多多包涵。我想制作一个ScrollView,其中包含具有相应名称(TextView)的图像。我希望能够通过在ScrollView 中触摸它来选择图像,但我不知道如何。我已经实现了这样的 ScrollView,我还希望能够添加带有名称的图片。

Main_activity.xml

<HorizontalScrollView
    android:id="@+id/scrollFilterView"
    android:layout_
    android:layout_
    android:scrollbars="none"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.57"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0">

    <LinearLayout
        android:id="@+id/gallery"
        android:layout_
        android:layout_
        android:orientation="horizontal" />
</HorizontalScrollView>

scrollview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_
    android:layout_
    android:orientation="vertical">

    <ImageView
        android:id="@+id/filterView"
        android:layout_
        android:layout_
        app:srcCompat="@android:drawable/sym_def_app_icon"
        android:contentDescription="filterView" />

    <TextView
        android:id="@+id/textFilter"
        android:layout_
        android:gravity="center"
        android:layout_
        android:text="textFilter" />

MainActivity.java

LayoutInflater inflater = LayoutInflater.from(this);

for (int i = 0; i < NUMBER_OF_FILTERS_GRID; i++ ) 

    View scrollView = inflater.inflate(R.layout.scrollview, gallery, false);

    TextView textview = scrollView.findViewById(R.id.textFilter);
    textview.setText("Filter "+ i);

    ImageView filterView = scrollView.findViewById(R.id.filterView);
    filterView.setImageResource(R.mipmap.ic_launcher);

    gallery.addView(scrollView);


如果这是否是正确的路径以及我应该使用哪些功能,请提供一些意见:)

【问题讨论】:

使用 RecycleView 代替 ScrollView 【参考方案1】:

使用 RecyclerView 而不是 ListView,并与适配器一起使用,这将授予您对列表中显示的每个项目的更多控制权(在本例中为 RecyclerView), 您将需要创建一个 Adapter 类、一个简单的 Items 类和一个新的 XML 布局,该布局将用作列表中每个项目的模型。

物品的简单类:

public class dataSetItem 

private Integer image;
private String listItemText;

public Integer getImage() 
return image;


public void setImage(Integer image) 
this.image = image;


public String getListItemText() 
return listItemText;


public void setListItemText(String listItemText) 
this.listItemText = listItemText;



在你的 mainActivity 中删除 listView 和 for 循环,改用这个:

  public class MyActivity extends Activity 
        private RecyclerView recyclerView;
        private RecyclerView.Adapter mAdapter;
        private RecyclerView.LayoutManager layoutManager;
        private ArrayList mAdapter = new ArrayList();

        @Override
        protected void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.my_activity);
            recyclerView = (RecyclerView) findViewById(R.id.your_recycler_view);

            mDataset.add(dataSetItem("item 1", R.mipmap.ic_launcher))
            mDataset.add(dataSetItem("item 2", R.mipmap.ic_launcher))

            // You can add more items to the mDataset list and call the adapter again to update the RecyclerView




            // use a linear layout manager
            layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);

            // specify an adapter (see also next example)
            mAdapter = new MyAdapter(myDataset);
            recyclerView.setAdapter(mAdapter);
        
        // ...
    

创建这个 XML 布局,它将作为每个项目的模型,称为 my_item_list_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" 
    android:layout_
    android:layout_>


            <LinearLayout
                android:layout_
                android:layout_
                android:orientation="horizontal"
                android:gravity="center">

                <ImageView
                    android:id="@+id/the_image_i_want_to_click"
                    android:layout_
                    android:layout_margin="10dp"
                    android:layout_
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp"
                    android:background="@color/ADRBlack"
                    />
                    <TextView
                        android:id="@+id/item_name"
                        android:layout_
                        android:layout_
                        android:maxWidth="220dp"
                        android:textColor="@color/Black"
                        android:textSize="16sp"
                        tools:text="Example Restaurant" />
                </LinearLayout>
</LinearLayout>

现在是适配器类,在本例中称为 MyAdapter:

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> 
        private customItem[] mDataset;

        // Provide a reference to the views for each data item
        // Complex data items may need more than one view per item, and
        // you provide access to all the views for a data item in a view holder
        public static class MyViewHolder extends RecyclerView.ViewHolder 
            // each data item is just a string in this case
            public TextView textView;
            public MyViewHolder(TextView v) 
                super(v);
                textView = v;
            
        

        // Provide a suitable constructor (depends on the kind of dataset)
        public MyAdapter(customItem[] myDataset) 
            mDataset = myDataset;
        

        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) 
            // create a new view
            TextView v = (TextView) LayoutInflater.from(parent.getContext())
                    .inflate(R.my_item_list_layout, parent, false);
                       

            MyViewHolder vh = new MyViewHolder(v);
            return vh;
        

        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) 
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            holder.textView.setText(mDataset[position].getListItemText);
            holder.textView.setBackground(mDataset[position].getImage);

            //Now to do something when you clic the image use this

            holder.textView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(final View v) 
            
                //Your action here

           
          );

        

        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() 
            return mDataset.length;
        
    

【讨论】:

以上是关于如何从 ScrollView 中的 ImageView 中选择项目?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 swift 中增加 ScrollView 中的 UITableView 高度

ScrollView:如何从文档中加载图像

如何在滚动 contentView 之前向上移动 scrollview 的框架? (当 scrollView 从屏幕中间开始时)

如何从 ScrollView 内的 RecyclerView 中移除焦点?

如何在ScrollView中嵌套ListView

以编程方式将 UiView 作为子视图