我有这个主视图,我有TableLayout
我以编程方式添加新视图。主视图如下(masterLayout.xml)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayoutActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:padding="5dp"
android:stretchColumns="*">
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:weightSum="2">
<ScrollView
android:id="@+id/queryScrollView"
android:layout_width="match_parent"
android:layout_span="2"
android:padding="5dp">
<TableLayout
android:id="@+id/queryTableLayoutActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:stretchColumns="*"></TableLayout>
</ScrollView>
</TableRow>
在上面的视图中,我向queryTableLayoutActivity
布局添加了新视图。要添加的布局如下(childLayout.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border"
android:orientation="vertical"
tools:context="com.teamtreehouse.oslist.ClassActivity">
<TextView
android:id="@+id/activeClassActivities"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|center_horizontal"
android:text="Active Class Activities"
android:textSize="18dp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="@+id/classNameActivity"
android:layout_width="@dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:background="#ccc"
android:textColor="#000"
android:textSize="@dimen/text_size"
android:layout_below="@+id/editClassActivity"
android:layout_alignRight="@+id/deleteActivity"
android:layout_alignEnd="@+id/deleteActivity" />
<TextView
android:id="@+id/courseNumberActivity"
android:layout_width="@dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/classNameActivity"
android:layout_below="@id/classNameActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="@dimen/text_size" />
<TextView
android:id="@+id/typeActivity"
android:layout_width="@dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/classNameActivity"
android:layout_below="@id/courseNumberActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="@dimen/text_size" />
<TextView
android:id="@+id/nameActivity"
android:layout_width="@dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/classNameActivity"
android:layout_below="@id/typeActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="@dimen/text_size" />
<TextView
android:id="@+id/dueDateActivity"
android:layout_width="@dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/classNameActivity"
android:layout_below="@id/nameActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="@dimen/text_size" />
<TextView
android:id="@+id/descriptionActivity"
android:layout_width="@dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/classNameActivity"
android:layout_below="@id/dueDateActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="@dimen/text_size" />
<TextView
android:id="@+id/maxGradeActivity"
android:layout_width="@dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/classNameActivity"
android:layout_below="@id/descriptionActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="@dimen/text_size" />
<TextView
android:id="@+id/enterGradeActivity"
android:layout_width="@dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/classNameActivity"
android:layout_below="@id/maxGradeActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="@dimen/text_size" />
<Button
android:id="@+id/editClassActivity"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="25dp"
android:background="@drawable/icon_document_edit"
android:onClick="editListener"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/deleteActivity" />
<Button
android:id="@+id/deleteActivity"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="wrap_content"
android:background="@drawable/icon_document_delete"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="deleteActivities"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Class Name :"
android:id="@+id/classNameDisplay"
android:textStyle="bold"
android:layout_alignTop="@+id/classNameActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/courseNumberActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Course ID :"
android:id="@+id/courseIDDisplay"
android:textStyle="bold"
android:layout_alignTop="@+id/courseNumberActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/courseNumberActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Course Type :"
android:id="@+id/courseTypeDisplay"
android:textStyle="bold"
android:layout_alignTop="@+id/typeActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/nameActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Activity Name :"
android:textStyle="bold"
android:id="@+id/activityNameDisplay"
android:layout_alignTop="@+id/nameActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/dueDateActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Due Date :"
android:textStyle="bold"
android:id="@+id/dueDateDisplay"
android:layout_alignTop="@+id/dueDateActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/descriptionActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Description :"
android:textStyle="bold"
android:id="@+id/descDisplay"
android:layout_alignTop="@+id/descriptionActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/maxGradeActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Maximum Grade :"
android:id="@+id/maxGradeDisplay"
android:textStyle="bold"
android:layout_alignTop="@+id/maxGradeActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/maxGradeActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Grade :"
android:id="@+id/gradeDisplay"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/enterGradeActivity" />
</RelativeLayout>
我使用以下代码
将childLayout.xml添加到masterLayout.xmlTableLayout queryTableLayout = (TableLayout) findViewById(R.id.queryTableLayoutActivity);
LayoutInflater inflater = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
String courseIDValue = c.getString(courseIdIndex);
View newTagView = inflater.inflate(R.layout.activity_class_activities, null);
queryTableLayout.addView(newTagView, activeClassActivityIndex++);
我想根据childLayout
字段对dueDateActivity
进行排序。我怎样才能做到这一点?
答案 0 :(得分:1)
首先,您不想对布局进行排序 - 您希望对使用特定布局充气的视图进行排序。所以没有“childLayout”有newTagView。 您的问题是一个更一般的问题形式:如何按特定顺序对父布局重新排序n个相对子视图。这不是关于改变他们的大小和位置(这很容易,你只是单独,因为你必须知道每个的所有新参数)这是关于重新排序它们。这个问题分为4:
注意:您不能只复制粘贴代码,需要根据您的具体需要进行更改(例如您需要的参数以及如何比较它)
使用
getChildCount()
要知道TableLayout子节点的数量,请使用
getChildAt(i)
遍历并访问它们。 现在制作将决定向新订单过渡的地图。
Map<View, Parameter> map = new HashMap<Integer, Parameter>();
for(int i = 0; i < queryTableLayout.getChildCount(); i++){
map.put(queryTableLayout.getChildAt(i),
queryTableLayout.getChildAt(i).findViewById(R.id.dueDateActivity)
.getParameter());
//since newTagView should inherit all RelativeLayout methods after infation
//getParameter() - the method from TextView(dueDateActivity) you need
现在要使订单正确,您需要按参数对地图进行排序, 在Java 8中看起来像
LinkedList<View> ordering = map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(new Comparator<Parameter>(){
/*implement your comparator*/
})/*now when stream is sorted collect it to list*/
.collect(Collectors.toList(Map.Entry::getKey);
现在您已经订购了
交换视图的最简单方法是交换它们的布局参数 就像在这个answer
中一样RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) b1.getLayoutParams();
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) b2.getLayoutParams();
b1.setLayoutParams(params2);
b2.setLayoutParams(params1);
创建一个方法void swap(View A,View B){...},它将适用于具有该代码的RelativeLayout View。
for(int i = 0; i < queryTableLayout.getChildCount(); i++)
swap(queryTableLayout.getChildAt(i), ordering.get(i));
如果你想要它们在已经排序的地方动态添加更容易你只需要获得正确的pos,并且如(1)中所示的params 和use
public void addView (View child, int index, ViewGroup.LayoutParams params)
答案 1 :(得分:1)
根据bedbad的4个步骤,我先跟随2,找到解决方案
我使用this回答来帮助我解决问题。 这是我的排序方法(按钮点击)
public void sortByWeek(View v){
queryTableLayout = (TableLayout) findViewById(R.id.queryTableLayoutActivity);
int childCount = queryTableLayout.getChildCount();
Map<View,String> map = new HashMap<View,String>();
for(int i = 0; i<childCount;i++){
map.put(queryTableLayout.getChildAt(i),((TextView)queryTableLayout.getChildAt(i).findViewById(R.id.dueDateActivity)).getText().toString());
}
Map<View,String> sortedMap = sortByComparator(map,sort);
ArrayList<View> viewSet = new ArrayList<>(sortedMap.keySet());
queryTableLayout.removeAllViews();
for(int i = 0;i<childCount;i++){
queryTableLayout.addView(viewSet.get(i));
}
if(sort){
sort = false;
}
}
public Map<View,String> sortByComparator(Map<View,String> map,final boolean order){
List<Map.Entry<View,String>> list = new LinkedList<Map.Entry<View,String>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<View, String>>() {
@Override
public int compare(Map.Entry<View, String> lhs, Map.Entry<View, String> rhs) {
if(order){
return lhs.getValue().compareTo(rhs.getValue());
}else{
return rhs.getValue().compareTo(lhs.getValue());
}
}
});
Map<View,String> sortedMap = new LinkedHashMap<View,String>();
for(Map.Entry<View,String> entry: list){
sortedMap.put(entry.getKey(),entry.getValue());
}
return sortedMap;
}