使用Android中的按钮单击在水平ListView中滚动ListView视图

时间:2015-11-17 09:40:16

标签: android listview horizontallist

我需要在点击"<"上滚动查看表单列表视图和">"目前正在展示。 我也得到了一些代码,但是当我使用它时,滚动整个列表视图。 我只需要滚动当前显示的视图。

  

屏幕

enter image description here

  

MainActivity.java

public class MainActivity extends Activity {
HorizontalListView listViewMovie;
HorizontalListAdapter adapter;
ArrayList<MovieListModel> listMovie;
Button btnMoveLeft, btnMoveRight;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    listViewMovie = (HorizontalListView) findViewById(R.id.hlvSimpleList);
    btnMoveLeft = (Button) findViewById(R.id.btnMoveLeft);
    btnMoveRight = (Button) findViewById(R.id.btnMoveRight);

    btnMoveLeft.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    btnMoveRight.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    listMovie = new ArrayList<MovieListModel>();
    MovieListModel movieDetail = new MovieListModel();
    movieDetail
            .setImgLink("http://imagine.inrialpes.fr/people/Damien.Rohmer/documents/publications/10_sigasia_wrinkle/thumbnail/thumbnail_movie.png");
    movieDetail.setName("Movie");
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);
    listMovie.add(movieDetail);

    adapter = new HorizontalListAdapter(MainActivity.this, listMovie);
    listViewMovie.setAdapter(adapter);

    listViewMovie.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, "Img:" + arg2,
                    Toast.LENGTH_SHORT).show();
        }
    });
  }
}
  

水平ListView适配器

public class HorizontalListAdapter extends ArrayAdapter<MovieListModel> {
LayoutInflater mInflater;
Context context;
ArrayList<MovieListModel> listMovie;
public HorizontalListAdapter(Context context,
        ArrayList<MovieListModel> listMovie) {
    super(context, R.layout.horizontal_list_col, listMovie);
    // TODO Auto-generated constructor stub
    this.context = context;
    this.listMovie = listMovie;
    mInflater = (LayoutInflater) getContext().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View rawView = mInflater.inflate(R.layout.horizontal_list_col, parent,
            false);
    ImageView img = (ImageView) rawView.findViewById(R.id.imgColMovie);
    TextView movieName = (TextView) rawView.findViewById(R.id.nameColMovie);
    ImageLoader.getInstance().displayImage(
            listMovie.get(position).getImgLink(), img,
            DisplayImageOption.getDisplayImage(),
            new AnimateFirstDisplayListener());
    movieName.setText(listMovie.get(position).getName().toString());
    return rawView;
  }
}
  

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal" >

    <Button
        android:id="@+id/btnMoveLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="&lt;" />

    <Button
        android:id="@+id/btnMoveRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=">" />
</LinearLayout>

<com.psk.cinemana.horizontallist.HorizontalListView
    android:id="@+id/hlvSimpleList"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:transcriptMode="alwaysScroll" />
</LinearLayout>

0 个答案:

没有答案