android模拟自定义视图上的Recycler视图

时间:2016-04-14 21:02:04

标签: android

我尝试在自定义视图中模拟RecyclerView个项目。我想创建简单的TimeLine视图,ListViewRecycler View不适合这个想法,例如:

enter image description here

基本上如何选择布局到根视图组?用于创建上面的自定义布局

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

        }
    }
}

感谢。

1 个答案:

答案 0 :(得分:0)

你看过RecyclerView.ItemDecoration了吗?您可以使用onDrawOver在项目之上绘制链接。