如何在android中使网格视图水平滚动

如何在android中使网格视图水平滚动

本文介绍了如何在android中使网格视图水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为 gridview 编写了代码,我可以在其中显示图像和文本,但我想在单个可滚动行中显示所有图像,如 Pulse 新闻应用程序.我已经在 xml 中为 gridview 实现了水平滚动视图,但它根本不起作用.我正在使用 pageviwer 作为标签,我正在使用片段.

I have written code for gridview in which i can show image and text but i want to show all image in single scrollable row like Pulse news apps.I have implemented horizontalscroll-view for gridview in xml but it does not work at all.I am using pageviwer for tabs and i am using fragments.

这是我的xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
   <HorizontalScrollView
   android:id="@+id/horizontalScrollView1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:fillViewport="true"
   android:scrollbars="horizontal" >
<GridView
    android:layout_width="500dp"
    android:layout_height="400dp"
    android:id="@+id/gridview"
    android:columnWidth="300dp"
    android:numColumns="3"
    android:horizontalSpacing="10dp"
    android:scrollbars="horizontal">
</GridView>
 </HorizontalScrollView>
</RelativeLayout>

这是我的图像适配器代码

Here is my image adpator code

public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
private TextView t;
public ImageAdapter(Context context, String[] mobileValues) {
    this.context = context;
    this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View gridView;
    if (convertView == null) {
        gridView = new View(context);
        // get layout from mobile.xml
        gridView = inflater.inflate(R.layout.showlist_item, null);
       // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.grid_item_label);
        textView.setText(mobileValues[position]);
        // set image based on selected text
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);
        String mobile = mobileValues[position];
        if (mobile.equals("Windows")) {
            imageView.setImageResource(R.drawable.test_play_image);
        } else if (mobile.equals("iOS")) {
            imageView.setImageResource(R.drawable.test_play_image);
        } else if (mobile.equals("Blackberry")) {
            imageView.setImageResource(R.drawable.test_play_image);
        } else {
            imageView.setImageResource(R.drawable.test_play_image);
        }
    } else {
        gridView = (View) convertView;
    }
    return gridView;
}
private void clickedButton(TextView tv){
    int num = Integer.parseInt(tv.getText().toString());
    ++num;
    tv.setText(Integer.toString(num));
}
private void clickedButtonm(TextView tv){
    int num = Integer.parseInt(tv.getText().toString());
    if(num>0){
        --num;
        tv.setText(Integer.toString(num));
    }
}
public int getCount() {
    return mobileValues.length;
}
public Object getItem(int position) {
    return null;
}
public long getItemId(int position) {
    return 0;
}
class MyOnClickListener implements OnClickListener{
    public final TextView tv;
    public MyOnClickListener(TextView tv){
        this.tv=tv;
    }
    public void onClick(View v) {
        // TODO Auto-generated method stub
        clickedButton(tv);
    }
}
class MyOnClickListenerm implements OnClickListener{
    public final TextView tv;
    public MyOnClickListenerm(TextView tv){
        this.tv=tv;
    }
    public void onClick(View v) {
        // TODO Auto-generated method stub
        clickedButtonm(tv);
    }
}

我想像这样向右滚动显示.

I want to display like this scrollable to right.

推荐答案

我发现了 两个-方法 GridView 在 github 上很有帮助.

I found Two-way GridView helpful on github.

它有一些方法:

scrollDirectionPortrait(垂直 | 水平)

scrollDirectionLandscape(垂直 | 水平)

numRows()

这篇关于如何在android中使网格视图水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:26