2016-04-14 20 views
0

RecyclerView ürününü simüle etmeye çalışıyorum. i örneğin, bu fikir için basit Timeline görünümü ve ListView veya Recycler View iyi değil oluşturmak ister:android simulate Özelleştirilebilir görünüm görüntüleyin özel görünümde

enter image description here

temelde Nasıl düzenleri viewgroup kökünü alabilirsiniz? için özel düzeni yukarıda oluşturmak

Benim xml görünümü:

<?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> 

Benim java kodu:

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 ve 2 aynı:

<?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> 

Benim Çözüm:

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); 

     } 
    } 
} 

Teşekkürler.

cevap

İlgili konular