Repeat the complex layout for each entry.

I have an application that queries the database and gets a set of records, on the display I need to present this data using a string, which is a complex layout. Each row contains some ImageViews, many TextViews, etc.

It is very difficult to create a line layout programmatically, is there a way to get the whole line layout (container and children of the line layout) from xml, edit some properties (for example, TextViews of the line layout) and add the result to LinearLayout?

+4
source share
3 answers

is there any way to get the whole row layout (container and row layout children) from xml

What you are looking for is an inflate view ( LayoutInflator )

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

HowTo: ListView, Adapter, getView, and various list items in a single ListView
http://android.amberfog.com/?p=296

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

... edit some properties (e.g. TextViews of the line layout) ...

Once you inflate a view, you can search for widgets in it so that you can manipulate it.

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

If your look is rather complicated and / or you often look for widgets inside it, I want to specify the ViewHolder technique shown in the example below, the corresponding 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 LinearLayout?

Presumably, you are already programmatically adding to LinearLayout , but if you want to see some code, here is an example that shows the setting of 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); ... 

Finally, you can do a lot (including custom strings) using the AdapterView / Adapter paradigm, for example. using a ListView with a SimpleCursorAdapter . It can save you some code by learning it. Some chat about it here:

Android ListView with different layouts for each row

+10
source

Generally, whenever you create an instance of a View using code, think twice.

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

+4
source

Which table are you using? If your layout is 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 the position of the desired child:

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

Source: https://habr.com/ru/post/1336957/


All Articles