gpt4 book ai didi

android - 如何将两个 TextView 添加到可扩展 ListView 的 subview

转载 作者:行者123 更新时间:2023-11-29 01:17:00 25 4
gpt4 key购买 nike

我试图将两个 TextView 并排放置在一个可扩展的 ListView 中。不幸的是,在堆栈溢出和谷歌搜索中找到的所有解决方案都被证明是徒劳的。

这个链接看起来很相似但显然不是解决方案 adding two textViews to the header of the expandable listview

我想要的是将两个 TextView 放置到子项而不是可扩展列表的标题。

我希望这两个 TextView 在 subview 上的样子 side by side textview format on expandable list

ExpandableListAdapter.java

  public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> ParentItem;
private HashMap<String, List<String>> ChildItem;


public ExpandableListAdapter(Context context, List<String> ParentItem,
HashMap<String, List<String>> ChildItem) {
this.context = context;
this.ParentItem = ParentItem;
this.ChildItem = ChildItem;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.ChildItem.get(this.ParentItem.get(listPosition))
.get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.child_item, null);
}

//TextView text1 = (TextView) convertView.findViewById(R.id.item1);
TextView text2 = (TextView) convertView.findViewById(R.id.item2);
//text1.setText("");
text2.setText(expandedListText);
return convertView;
}

@Override
public int getChildrenCount(int listPosition) {
return this.ChildItem.get(this.ParentItem.get(listPosition))
.size();
}

@Override
public Object getGroup(int listPosition) {
return this.ParentItem.get(listPosition);
}

@Override
public int getGroupCount() {
return this.ParentItem.size();
}

@Override
public long getGroupId(int listPosition) {
return listPosition;
}

@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.parent_item, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);

TextView location = (TextView) convertView
.findViewById(R.id.item1);
location.setTypeface(null, Typeface.BOLD);
location.setText("whatever you want");
return convertView;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}


public static HashMap<String, List<String>> getData() {
HashMap<String, List<String>> ParentItem = new HashMap<String,List<String>>();

List<String> NADMO = new ArrayList<String>();
NADMO.add("38838333");
NADMO.add("03220-34084");
NADMO.add("03820-23110");
NADMO.add("03222-22145");
NADMO.add("03120-46676");
NADMO.add("03720-23154");
NADMO.add("03920-22657");
NADMO.add("020-2006943/03620-27158");
NADMO.add("03321-32127");
NADMO.add("03520-27271");

List<String> GPS = new ArrayList<String>();
GPS.add("Lion");
GPS.add("Tiger");
GPS.add("Leopard");
GPS.add("Cheetah");
GPS.add("Bear");

List<String> TPS = new ArrayList<String>();
TPS.add("Cricket");
TPS.add("Football");
TPS.add("Tennis");
TPS.add("Basket Ball");
TPS.add("Base Ball");


ParentItem.put("NADMO"), NADMO);
ParentItem.put("GPS", GPS);
ParentItem.put("TPS", TPS);

return ParentItem;
}
}

MainActivity.java

public class CodeDir extends AppCompatActivity {
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;

@Override
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);

setContentView(R.layout.activity_code_dir);

//TOOLBAR SUPPORT - Set a Toolbar to replace the ActionBar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);


expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListDetail = ExpandableListAdapter.getData();
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new ExpandableListAdapter(this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " ListView Open.",
Toast.LENGTH_SHORT).show();
}
});

expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " ListView Closed.",
Toast.LENGTH_SHORT).show();

}
});

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(getApplicationContext(),
expandableListDetail.get(
expandableListTitle.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT
)
.show();
return false;
}
});
}

}

child_item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:padding="2dp">

<TextView
android:id="@+id/item1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:layout_weight="1.0"
android:paddingBottom="10dp"
android:text="Left Side"
android:textSize="20sp"/>


<TextView
android:id="@+id/item2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="10dp"
android:layout_weight="1.0"
android:paddingBottom="10dp"
android:text="Right side"
android:textSize="20sp"/>

</LinearLayout>
</LinearLayout>

最佳答案

这可能会有所帮助。以编程方式创建相对布局,将 TextView 添加到相对布局并将相对布局添加到预期 View 。

是这样的:RelativeLayout relativeLayout = new RelativeLayout(this);RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, RelativeLayout.LayoutParams.WRAP_CONTENT);

parentView.addView(relativeLayout);

关于android - 如何将两个 TextView 添加到可扩展 ListView 的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38912032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com