0
私のカスタムビューにRecyclerView
アイテムをシミュレートしようとしています。私は例えば、単純なタイムラインビューとListView
またはRecycler View
このアイデアのためによくないを作成したい:アンドロイドはカスタムビューでRecyclerビューをシミュレートします
基本的にどのように私はのViewGroup rootにレイアウトを選ぶことができますか?以下のためのカスタムレイアウト上
私のXMLビューを作成します。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ir.pishguy.timelineview.MainActivity">
</RelativeLayout>
私のJavaコード:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup inclusionViewGroup = (ViewGroup)findViewById(R.id.root_layout);
View child1 = LayoutInflater.from(this).inflate(
R.layout.child_layout1, null);
View child2 = LayoutInflater.from(this).inflate(
R.layout.child_layout2, null);
inclusionViewGroup.addView(child1);
inclusionViewGroup.addView(child2);
}
}
child_layout 1と2は同じです。
<?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:background="#909090"
android:layout_height="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ddddd"/>
</LinearLayout>
マイソリューション:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int placeholderId = R.id.reservedNamedId; // placeholderId==1
ViewGroup placeholder = (ViewGroup)findViewById(placeholderId);
for (int i=0; i<3; i++){
View child1 = LayoutInflater.from(this).inflate(
R.layout.child_layout1, null);
child1.setId(i);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.reservedNamedId);
child1.setLayoutParams(p);
placeholder.addView(child1);
}
}
}
ありがとうございます。