我正在使用Android应用,并且使用gridview来显示文本。 某些文本条目可能很大,因此,我想使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!");
}
});
答案 0 :(得分:0)
将您的单元格更改为此
<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;
}
}