简体   繁体   中英

How to make cells of GridView Scrollable?

I'm working on an Android App and I use a to display text. Some entries of text may be large so, I want to make the cells of the gridview scrollable.

I know how to make a TextView scrollable, you just have to set

android:scrollbars = "vertical"

property of your TextView in your xml file and use

yourTextView.setMovementMethod(new ScrollingMovementMethod());

in your code.

The xml file of each cell of the GridView is this:

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

and the method getView() in GridView's Adapter is this:

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

So, I thought that all that needed was to set the same property as above to the TextView in the xml file of the GridView cell, and use setMovementMethod() inside getView() of the Adapter, but it is not working.

Any ideas?

Thanks a lot.

EDIT:

Here is the full code of the Adapter:

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

and the code of the ClickListener of the GridView:

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

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

            }
        });

change your cell into this

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

EDIT: change your adapter into this: the text is scrolling and it is clickable.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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