【问题标题】:How to make cells of GridView Scrollable?如何使 GridView 的单元格可滚动?
【发布时间】:2018-06-26 20:25:19
【问题描述】:

我正在开发一个 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?

标签: gridview android gridview


【解决方案1】:

把你的单元格改成这个

<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;
        }
    }

【讨论】:

  • 我试过了,但是现在 GridView 的 ItemClickListener 没有被调用。
  • 你的滚动问题解决了吗?我可以看看你的 gridViewAdapter 整个代码吗?还有 ItemClickListener
  • No ti has not :(
  • 我在我的问题中提供了您作为 EDIT 提出的代码。
  • @NikosKardoulakis 我已经成功了,我会更新我的答案
猜你喜欢
  • 2018-02-27
  • 1970-01-01
  • 2015-08-04
  • 2013-01-06
  • 2011-05-30
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多