在CustomListAdapter的getView方法中更改TextView GRAVITY

时间:2015-03-16 10:40:54

标签: android listview adapter custom-adapter

我有一个带有三个textviews的customListAdapter我想要的是当满足某些条件时,所有三个textview的GRAVITY都应该反转,如代码中的注释所示。我只发布customAdapter而不是剩余的代码,因为它是不可能的。

 public class CustomListAdapter extends BaseAdapter {
private ArrayList<NewsItem> listData;
private LayoutInflater layoutInflater;

public CustomListAdapter(Context aContext, ArrayList<NewsItem> listData)     {
    this.listData = listData;
    layoutInflater = LayoutInflater.from(aContext);
}

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

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_row_layout,      null);
        holder = new ViewHolder();
        holder.headlineView = (TextView)    convertView.findViewById(R.id.title);
        holder.reporterNameView = (TextView) convertView.findViewById(R.id.reporter);
        holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    if (some condition true){
   // reverse GRAVITY of all TEXTVIEWS
  }
    holder.headlineView.setText(listData.get(position).getHeadline());

    holder.reporterNameView.setText("By, " +      listData.get(position).getReporterName());
    holder.reportedDateView.setText(listData.get(position).getDate());
    return convertView;
}

static class ViewHolder {
    TextView headlineView;
    TextView reporterNameView;
    TextView reportedDateView;
}

}

3 个答案:

答案 0 :(得分:0)

在你的条件下设置引力:

if (some condition true){
holder.headlineView.setGravity(Gravity.LEFT);
}

答案 1 :(得分:0)

你可以这样做

holder.headlineView.setGravity(Gravity.CENTER);
holder.reporterNameView.setGravity(Gravity.CENTER|Gravity.RIGHT);

希望这会有所帮助。!!

答案 2 :(得分:0)

在if语句中,您可以通过编程方式更改重力:

if (some condition true){
   // reverse GRAVITY of all TEXTVIEWS
   holder.headlineView.setGravity(Gravity.CENTER | Gravity.BOTTOM);
   holder.reporterNameView.setGravity(Gravity.LEFT| Gravity.TOP);
   holder.reportedDateView.setGravity(Gravity.RIGHT| Gravity.BOTTOM);
}