2016-08-12 6 views
2

2つのテキストビューを展開可能なリストビューに並べて配置しようとしています。残念ながら、スタックオーバーフローで見つかったすべてのソリューションとGoogleの検索は役に立たないことが判明しました。拡張可能リストビューの子に2つのテキストビューを追加する方法

このリンクは似ていますが、どうやら私が欲しいものソリューション adding two textViews to the header of the expandable listview

は子供ではない拡張可能なリストのヘッダーに2つのテキストビューを配置することではありません。

私は2つのテキストビューは、子ビューに見えるようにする方法の画像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> 
+0

彼らは現在どのように見ていますか?現在のスクリーンショットも追加しますか? –

+0

@SohailZahidはい、拡張可能なリストの子にあります –

答えて

1

これは役立つかもしれません。相対レイアウトをプログラムで作成し、相対レイアウトにテキストビューを追加し、相対レイアウトを予想されるビューに追加します。

このようなもの: RelativeLayout relativeLayout = new RelativeLayout(this); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width、RelativeLayout.LayoutParams.WRAP_CONTENT);

parentView .addView(relativeLayout);

関連する問題