开发者

Repeat a complex Layout for each record

I have an app that queries a database and get a recordset, on the display, I need to present these data with a row that is a complex layout. Each row contains some ImageView, many TextView etc...

It's really difficult to create the row layout programmatically, is there any way to get the entire row layout (container and children of the row layout) from an xml, edit som开发者_如何学JAVAe of the properties (like the TextViews of the row layout) and add the result to a LinearLayout?


is there any way to get the entire row layout (container and childs of the row layout) from an xml

What you are looking for is how to inflate a view (LayoutInflator)

Now that you have the right term, it should be easy to find examples, inflate is popular in ListView tutorials. For an example, take a look at at getView() in this tutorial for an example:

HowTo: ListView, Adapter, getView and different list items’ layouts in one ListView
http://android.amberfog.com/?p=296

mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
...
convertView = mInflater.inflate(R.layout.item1, null);

... edit some of the properties (like the TextViews of the row layout) ...

Once you inflate the view, you can search for the widgets within it, so you can manipulate it.

holder.textView = (TextView) convertView.findViewById(R.id.text);

If your view is fairly complex and/or you will often be looking for widgets within it, I want to point out the ViewHolder technique, shown in the example referenced, relevant bits below:

// Data structure to save lookups
public static class ViewHolder {
    public TextView textView;
}
...
// Save lookups to widgets for this view in ViewHolder in tag
ViewHolder holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.text);
view.setTag(holder);
...
// Grab saved widgets - no need to search tree for them via lookup again
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.textView.setText(mData.get(position));

... and add the result to a LinearLayout?

Presumably, you're already programatically adding to the LinearLayout, but if you want to see some code, here's an example that shows setting some layout parameters:

Android LinearLayout
http://developerlife.com/tutorials/?p=312

  // main "enclosing" linearlayout container - mainPanel
  final LinearLayout mainPanel = new LinearLayout(ctx);
  {
    mainPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                               LayoutParams.FILL_PARENT));
    mainPanel.setOrientation(LinearLayout.VERTICAL);
    ...
  }
  ...
  // top panel
  LinearLayout topPanel = new LinearLayout(ctx);
  {
    // WEIGHT = 1f, GRAVITY = center
    topPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                              LayoutParams.WRAP_CONTENT,
                                              1));
    ...
  }
  ...
  // bottom panel
  LinearLayout bottomPanel = new LinearLayout(ctx);
  {
    LayoutUtils.Layout.WidthFill_HeightWrap.applyLinearLayoutParams(bottomPanel);
    ...
  }
  ...    
  // add the panels
  mainPanel.addView(topPanel);
  mainPanel.addView(bottomPanel);
  ...

Lastly, you can do alot (including custom rows) with the AdapterView / Adapter paradigm, e.g. using ListView with a SimpleCursorAdapter. It may save you some code by looking into it. Some babbling about it here:

Android ListView with different layouts for each row


As a rule of thumb, whenever you create an instance of View using code, think twice.

Review LayoutInflater on how to create layout from resources. In your special case, you might also want to check ResourceCursorAdapter if you want to show multiple rows in a ListView.


What kind of table are you using? If your layout would be something like this:

<TableLayout>
  <LinearLayout android:id="@+id/linearLayoutTest">
    <TextView/>  
    <TextView ....
    <ImageView .... 
  </LinearLayout>
  <LinearLayout ....
</TableLayout>

It would be easy to access the row container through the id and then find by position the desirable child:

LinearLayout linearLayout = (LinearLayout)this.findViewById(R.id.linearLayoutTest);
TextView textView = (TextView)linearLayout.getChildAt(0);
textView.setText("New Text");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜