繁体   English   中英

如何使GridView的单元格可滚动?

[英]How to make cells of GridView Scrollable?

我正在使用Android应用程序,并且使用来显示文本。 某些文本条目可能很大,因此, 我想使gridview的单元格可滚动。

我知道如何使TextView可滚动,您只需设置

android:scrollbars = "vertical"

xml文件中TextView的属性并使用

yourTextView.setMovementMethod(new ScrollingMovementMethod());

在您的代码中。

GridView的每个单元格的xml文件是这样的:

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

    <TextView
        android:id="@+id/grid_cell_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center_vertical|center_horizontal"
        android:text="TextView"
        android:scrollbars = "vertical"/>

</RelativeLayout> 

GridView的Adapter中的getView()方法是这样的:

public View getView(int position, View convertView, ViewGroup parent) {
        final String a = listStorage.get(position);
        // view holder pattern
        if (convertView == null) {
            final LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.grid_cell, parent, false);

            TextView text = (TextView) convertView.findViewById(R.id.grid_cell_text);
            final ViewHolder viewHolder = new ViewHolder(text);
            convertView.setTag(viewHolder);
        }

        final ViewHolder viewHolder = (ViewHolder)convertView.getTag();
        viewHolder.text.setText(a);
        viewHolder.text.setTextColor(Color.WHITE);

        // Set height and width constraints for the text view
        viewHolder.text.setLayoutParams(new RelativeLayout.LayoutParams(GridView.AUTO_FIT, dp_to_px(40)));
        return convertView;
    } 

因此,我认为所需要做的就是为GridView单元格的xml文件中的TextView设置与上述相同的属性,并在Adapter的getView()中使用setMovementMethod(),但它不起作用。

有任何想法吗?

非常感谢。

编辑:

这是适配器的完整代码:

public class CustomAdapter extends BaseAdapter {
    private LayoutInflater layoutinflater;
    private List<String> listStorage;
    private Context context;
    String msg = "drag";
    Resources r;

    public CustomAdapter(Context context, List<String> customizedListView, Resources r) {
        this.context = context;
        layoutinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        listStorage = customizedListView;
        this.r = r;
    }

    @Override
    public int getCount() {
        return listStorage.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final String a = listStorage.get(position);
        // view holder pattern
        if (convertView == null) {
            final LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.grid_cell, null);

            final TextView text = (TextView) convertView.findViewById(R.id.grid_cell_text);

            final ViewHolder viewHolder = new ViewHolder(text);
            convertView.setTag(viewHolder);
        }

        final ViewHolder viewHolder = (ViewHolder)convertView.getTag();
        viewHolder.text.setText(a);
        viewHolder.text.setLayoutParams(new RelativeLayout.LayoutParams(GridView.AUTO_FIT, dp_to_px(40)));
        return convertView;
    }

    // Your "view holder" that holds references to each subview
    private class ViewHolder {
        private final TextView text;

        public ViewHolder(TextView text) {
            this.text = text;
        }
    }

    public int dp_to_px(int dp){
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
        return px;
    }
}

以及GridView的ClickListener的代码:

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Log.d(msg, "Click!");

            }
        });

把你的细胞变成这个

<ScrollView
    android:id="@+id/scrollView"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <TextView
            android:id="@+id/grid_cell_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center_vertical|center_horizontal"
            android:text="TextView"
            android:scrollbars = "vertical"/>
    </RelativeLayout> 
</ScrollView>

编辑:将您的适配器更改为此:文本正在滚动并且它是可单击的。

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final String a = listStorage.get(position);
        // view holder pattern
        if (convertView == null) {
            final LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.grid_cell, null);

            final TextView text = (TextView) convertView.findViewById(R.id.grid_cell_text);
            ScrollView scrollView = (ScrollView) convertView.findViewById(R.id.scrollView);

            final ViewHolder viewHolder = new ViewHolder(text, scrollView);
            convertView.setTag(viewHolder);
        }

        final ViewHolder viewHolder = (ViewHolder)convertView.getTag();
        viewHolder.text.setText(a);
        viewHolder.scrollView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, dp_to_px(60)));
        viewHolder.text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("CLICKITEM", String.valueOf(position));
            }
        });
        return convertView;
    }

    // Your "view holder" that holds references to each subview
    private class ViewHolder {
        private final TextView text;
        private final ScrollView scrollView;

        public ViewHolder(TextView text,ScrollView scrollView) {
            this.text = text;
            this.scrollView = scrollView;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM