【问题标题】:onclick issue on Linear layout in HorizontalScrollViewHorizo​​ntalScrollView 中线性布局的 onclick 问题
【发布时间】:2016-05-31 13:58:31
【问题描述】:

我有一个RelativeLayout,里面有一个HorizontalScrollView,里面有10 个线性布局的图像。我想在 RelativeLayout 上设置侦听器,而不是在图像上我是如何做到的。

下面是我的代码。

<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_below="@+id/ortext">

<HorizontalScrollView
     android:id="@+id/hscroll"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginBottom="5dp"
    android:background="#f4ede7"
    android:focusable="false"
    android:scrollbars="@null"
    android:clickable="false"
    android:focusableInTouchMode="false" >

    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false" >

        <ImageView
            android:id="@+id/img1"
            android:layout_width="200dp"
            android:layout_height="90dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/img2"
            android:layout_width="200dp"
            android:layout_height="90dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/image1" />

        <ImageView
            android:id="@+id/img3"
            android:layout_width="200dp"
            android:layout_height="90dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/image1" />

        <ImageView
            android:id="@+id/img4"
            android:layout_width="200dp"
          android:layout_height="90dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/image1" />

        <ImageView
            android:id="@+id/img5"
            android:layout_width="200dp"
            android:layout_height="90dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/image1" />

        <ImageView
            android:id="@+id/img6"
            android:layout_width="200dp"
            android:layout_height="90dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/image1" />
    </LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

【问题讨论】:

  • 使用 onTouchListener 来控制小部件的触摸。

标签: android android-layout horizontalscrollview android-relativelayout


【解决方案1】:

RelativeLayout上设置onClickListener

RelativeLayout rlMain=findViewById(R.id.rl_main);
rlMain.setOnClickListner(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        //Your code here
    }
});

在此之前从RelativeLayout中删除

android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"

让它看起来像

<RelativeLayout 
    android:id="@+id/rl_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ortext">

并添加android:clickable="false"RelativeLayout 的所有其他子项

【讨论】:

    【解决方案2】:

    如果您想将侦听器添加到您的 RelativeLayout,这与您将侦听器设置为视图的方式类似。

        RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativelayout);
        rl.setOnClickListner(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            //Your code here
        }
    });
    

    【讨论】:

      【解决方案3】:

      使用 RecyclerView 代替 Horizo​​ntalScrollView

      然后您可以通过 decalar 自定义 recyclerview 适配器轻松获取 imageview 的 onClick 列表。

      在 bindView 中制作点击监听器

      在 gradle 中添加这一行

      {
       compile 'com.android.support:recyclerview-v7:23.1.1'
      }
      

      然后在您的 xml 文件 (activity_main.xml) 中添加回收站视图

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >
      
      <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
      
      </RelativeLayout>
      

      然后在MainActivity.java RecyclerView中

      public class ListActivity extends AppCompatActivity {
      
      
       ArrayList<String> imagesUrls = new ArrayList<>();
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           // ...
           // Lookup the recyclerview in activity layout
           RecyclerView rvImages = (RecyclerView) findViewById(R.id.recycler_view);
      
       //get images from your method
       imagesUrls = getImageUrls();
      
      
      
         // Set layout manager to position the items
          rvImages .setLayoutManager(new LinearLayoutManager(this));
      
           // Create adapter passing in the sample user data
           ImageViewAdapter adapter = new ImageViewAdapter (MainActivity.this,);
           // Attach the adapter to the recyclerview to populate items
           rvImages .setAdapter(adapter);
           // That's all!
           }
      }
      

      适配器类

      package yourpackage.name;
      import android.content.Context;
      import android.support.v7.widget.RecyclerView;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.TextView;
      import java.util.ArrayList;
      
      
      public class ImageViewAdapter extends RecyclerView.Adapter<ImageViewAdapter .ViewHolder> {
      
      ArrayList<String> productList = new ArrayList<String>();
      Context mContext;
      
      @Override
      public int getItemCount() {
          return productList.size();
      }
      
      public ImageViewAdapter (Context context, ArrayList<String> item) {
          this.productList = item;
          this.mContext = context;
      }
      
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
          View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_imageview_row, parent, false);
          return new ViewHolder(v);
      }
      
      public class ViewHolder extends RecyclerView.ViewHolder {
      
          ImageView imgPeoples;
      
          public ViewHolder(View itemView) {
              super(itemView);
              imgPeoples = (ImageView) itemView.findViewById(R.id.img);
      
          }
      }
      
      @Override
      public void onBindViewHolder(ViewHolder holder, final int position) {
      
      //Set image to imageview 
      
      //@Note: here im using universal image loader to load image from url to imageview.
      
      //Utility.setImageUniversalLoader is my methoad to load image from imageurl
      
         Utility.setImageUniversalLoader(mContext,productList.get(position),holder.imgPeoples);
      
           }
      
        }
      

      在布局文件夹中使用 imageview 创建 single_imageview_row.xml

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        相关资源
        最近更新 更多