【问题标题】:Add fragment as Subview in android在android中添加片段作为子视图
【发布时间】:2014-06-25 20:04:18
【问题描述】:

我有一个具有水平滚动视图的片段。我想向这个滚动视图添加另一个片段。我怎样才能做到这一点?

这是我的第一个片段

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="220dp"
    android:background="@drawable/arrow_bg">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/horizontalScrollView"
        android:layout_marginTop="15dp" >

        <LinearLayout
            android:id="@+id/offerLayout"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"></LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

我正在尝试将以下片段添加到滚动视图中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="174dp"
    android:layout_height="214dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="HEALTH"
        android:id="@+id/offerTitle"
        android:capitalize="characters" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/offer_image_text_bg">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="135dp"
            android:id="@+id/offerIcon"
            android:src="@drawable/health_img"/>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:id="@+id/favLayout"
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <RatingBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/ratingBar"
                    android:numStars="5"
                    android:rating="4"
                    android:isIndicator="true"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="5dp"
                    style="@style/ratingBarStyle"/>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_marginRight="5dp"
                        android:id="@+id/favIcon"
                        android:src="@drawable/like_icon"
                        android:layout_alignParentRight="true"/>
                    </RelativeLayout>


            </LinearLayout>

            <LinearLayout
                android:layout_below="@+id/favLayout"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Medium Text"
                    android:id="@+id/txt_OfferTitle2" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Small Text"
                    android:id="@+id/txt_OfferDesc" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

这是我正在使用的代码

private void loadSelectedOffers() {
        for(int i=0;i<3;i++)
        {
            OfferItemFragment offerItem = new OfferItemFragment();
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            offerItem.getView().setLayoutParams(params);
            offerLayout.addView(offerItem.getView());
        }
    }

但是我在第

行遇到了一个空指针异常

offerItem.getView()

我怎样才能正确地做到这一点?

【问题讨论】:

    标签: android android-layout android-fragments android-view


    【解决方案1】:

    将 FrameLayout 添加到您的第一个片段,然后将该片段添加到该框架布局。

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parent"
        android:orientation="vertical" android:layout_width="wrap_content"
        android:layout_height="220dp"
        android:background="#468494">
    
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/horizontalScrollView"
            android:background="#BB099D"
            android:layout_marginTop="15dp" >
    
            <LinearLayout
                android:id="@+id/child"
                android:orientation="horizontal" 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/black">
             </LinearLayout>
    
        </HorizontalScrollView>
    </LinearLayout>
    

    然后在您的代码中使用FragmentManagerFragmentTransaction 将片段添加到视图中

    public class MainActivity extends Activity {
    
    Fragment f;
    EditText send;
    LinearLayout parent;
    LinearLayout child;
    HorizontalScrollView scroll;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        ViewGroup vg = (LinearLayout) inflater.inflate(R.layout.activity_main, null );
    
        child = (LinearLayout) vg.findViewById(R.id.child);
        loadSelectedOffers();
        setContentView(vg);
    
    }
    
    
    private void loadSelectedOffers() {
        for(int i=0;i<3;i++)
        {
            FrameLayout frame = new FrameLayout(this);
            child.addView(frame);
            frame.setId(i);
            frame.setBackgroundColor(getResources().getColor(R.color.black));
            frame.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            SenderFragment offerItem = new SenderFragment();
            transaction.add(i, offerItem, "offer_item_" );
            transaction.commit();
    
    
    
        }
    }
    

    我相信这样的事情现在应该可以工作了。

    【讨论】:

    • 感谢您的回答。这个答案有效,但这不是我想要的。此代码块在另一个之上添加了一个 OfferItemFragment。但我需要一个接一个的滚动视图作为水平列表。
    • 您知道您的视图中有多少项目吗?会是 3 吗?
    • 所以,我仍在考虑一种方法来根据您的需要动态加载页面。但替代使用滚动视图的替代方法是使用水平方向的列表视图。您可以使用 OfferLayout 将每个项目基于列表视图内部并以这种方式填充数据
    • 放弃这个想法。我不相信您实际上可以制作水平列表视图。
    • 我相信我有一个答案可以真正做你想做的事。我已经更新它以反映这些变化。让我知道它是否适合您。
    【解决方案2】:

    你得到的 view null 是因为它的 onCreateView() 还没有被执行,这反过来又是因为你只是实例化了 Fragment 而没有将它附加到你的活动中。 尝试在 HSV 中添加 FrameLayouts 并用片段替换它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多