在List View适配器中通过post方法设置LayoutParams

时间:2015-07-16 03:37:14

标签: android

我正在尝试在文本视图中设置边距,并在列表视图适配器中以线性布局填充。基本上,如果文本视图有2行或更少的文本行,我正在为该列表项创建边距/填充。

以下是代码:

public class StockCountListAdapter extends ArrayAdapter<StockCountItem> {

private TextView txtProduct;
private LinearLayout llStockCountItem;

public StockCountListAdapter(Context context, int textViewResourceId, List<StockCountItem> objects) {
    super(context, textViewResourceId, objects);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.stock_count_list_item, parent, false);

    txtProduct = (TextView) rowView.findViewById(R.id.stock_count_item_product);
    llStockCountItem = (LinearLayout) rowView.findViewById(R.id.ll_stock_count_item);

    StockCountItem item = getItem(position);

    txtProduct.setText(item.Product);

    llStockCountItem.post(new Runnable() {
       @Override
        public void run() {
           if (txtProduct.getLineCount() <= 2) {

               llStockCountItem.setPadding(0, 0, 0, 10);

               LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 7);
               params.setMargins(1, 1, 1, 10);
               txtProduct.setLayoutParams(params);

           }
       }
    });

    return rowView;
}

滚动列表时应用边距/填充,但在首次显示列表或更改方向时不应用。

如何在活动负载或方向更改上应用保证金/填充?

2 个答案:

答案 0 :(得分:0)

处理运行时更改

当屏幕方向发生此类更改时,Android会重新启动正在运行的活动(onDestroy(),然后调用onCreate())。重新启动行为旨在通过使用与新设备配置匹配的备用资源自动重新加载应用程序来帮助您的应用程序适应新配置。

要正确处理重新启动,您的活动必须通过正常的活动lifecycle恢复之前的状态,在此活动中Android会在销毁您的活动之前调用onSaveInstanceState(),以便您可以保存有关的数据申请状态。然后,您可以在onCreate()onRestoreInstanceState()期间恢复状态。

http://developer.android.com/guide/topics/resources/runtime-changes.html

答案 1 :(得分:0)

代码

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,
                         LinearLayout.LayoutParams.WRAP_CONTENT, 7);
params.setMargins(1, 1, 1, 10);
params.gravity = Gravity.FILL; // this is where i come in
txtProduct.setLayoutParams(params);
txtProduct.invalidate(); // edits
txtProduct.requestLayout();//edits

Gravity的类型现在取决于您,您需要覆盖onOnrientationChanged()(类似的东西)并在那里调用相同的代码

修改

基于你的post runnable代码我正在考虑一个解决方法,我不认为它会起作用但值得...因为txtProduct.getLineCount()在布局通过后返回0,我即兴创作

if(txtProduct.getLineCount()==0){ //if false you run your previous code without post
     int textSize = txtProduct.getTextSize()/2,mw,lines;
     // for the textsize i am thinking of a rectangle
     if(txtProduct.getMeasuredWidth() > 0) //if this line checks out 0 the code is trash

  //assume none where zero
  int fillW = textSize * txtProduct.getText().toString().length();
  if(fillW > mw){ //my logic here is we have more than one line
     if(fillW / mw > -1){ //
      lines = fillW/mv;
       if(fillW%mv !=0){// it is not a factor which means there is another line trending
           lines++;
           // now add your normal code here without post to check if lines is what you want
       }
     }
   }
 }else{ //add your code without post

看看是否有效,