在Horizo​​ntalScrollView中的线性布局上的onclick问题

时间:2016-02-19 05:13:33

标签: android android-layout horizontalscrollview android-relativelayout

我有一个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>

3 个答案:

答案 0 :(得分:0)

onClickListener

上设置RelativeLayout
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

的所有其他孩子

答案 1 :(得分:0)

如果要向RelativeLayout添加侦听器,则类似于将Listners设置为视图的方式。

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

答案 2 :(得分:0)

使用RecyclerView而不是Horizo​​ntalScrollView

然后您可以通过声明自定义的recyclerview适配器轻松获取用于您的imageview的onClick listner。

在bindView中制作单击侦听器

在gradle中添加此行

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

然后在xml文件(activity_main.xml)中添加recycleler视图

<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