0

I use HorzListView in sileria library (Aniqroid 3.5)

In the ListView contain ImageView, it doesn't scroll when I touch in ImageView, it just scrolls when I touch in gap between 2 ImageView. TextView doesn't have this problem. How to fix it?

My code:

activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.sileria.android.view.HorzListView
        android:id="@+id/horizontal_lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <com.sileria.android.view.HorzListView
        android:id="@+id/horizontal_lvImg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

item_horizontal_listview.xml

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

    <ImageView
        android:id="@+id/imgItem"
        android:layout_width="200dp"
        android:layout_height="100dp" />

</LinearLayout>

HorizontalListViewAdapter.java

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class HorizontalListViewAdapter extends BaseAdapter {
    private int[] arrImg;
    private Context context;

    public HorizontalListViewAdapter(Context context, int[] arrImg) {
        this.arrImg = arrImg;
        this.context = context;
    }

    @Override
    public int getCount() {
        return arrImg.length;
    }

    @Override
    public Object getItem(int arg0) {
        return arrImg[arg0];
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int arg0, View convertView, ViewGroup arg2) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.item_horizontal_listview,
                    null);
        }

        ImageView imgItem = (ImageView) convertView.findViewById(R.id.imgItem);
        imgItem.setImageResource(arrImg[arg0]);

        return convertView;
    }

}

MainActivity.java

import com.sileria.android.view.HorzListView;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // listview don gian
        HorzListView listview = (HorzListView) findViewById(R.id.horizontal_lv);
        String[] arrString = { "Android", "Windown Phone", "IOS", "Mac",
                "Linux" };
        ArrayAdapter<String> adapterString = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, arrString);
        listview.setAdapter(adapterString);

        // listview Item la anh
        HorzListView listviewImg = (HorzListView) findViewById(R.id.horizontal_lvImg);
        int[] arrImg = { R.drawable.img1, R.drawable.img2, R.drawable.img3,
                R.drawable.img4 };
        HorizontalListViewAdapter adapterImg = new HorizontalListViewAdapter(
                this, arrImg);
        listviewImg.setAdapter(adapterImg);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Tấn Nguyên
  • 1,607
  • 4
  • 15
  • 25
  • Any click listeners on the image? What's the code that's causing the problem? – s-hunter Apr 02 '16 at 19:57
  • @s-hunter yes, any on touch/click listener on the image purpose to scroll left/right, it doesn't scroll :( I think it's library problem, because I'm using simple code :( I edited the post ! – Tấn Nguyên Apr 04 '16 at 00:11
  • If there is no particular reason that you have to use this com.sileria.android.view.HorzListView, I would suggest to use RecyclerView, Check out this answer, http://stackoverflow.com/a/28460399/2069407 – s-hunter Apr 04 '16 at 01:21

0 Answers0