我有一个具有水平滚动视图的片段。我想在此scrollview中添加另一个片段。我怎样才能做到这一点?
这是我的第一个片段
<?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>
我正在尝试将以下片段添加到scrollview
<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()
我该如何正确地做到这一点?
答案 0 :(得分:1)
将FrameLayout添加到第一个片段,然后将片段添加到该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>
然后在您的代码中使用FragmentManager
和FragmentTransaction
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();
}
}
我相信这样的事情现在应该有效。
答案 1 :(得分:1)
您正在获取视图null,因为它的onCreateView()尚未执行,这反过来是因为您只是实例化了片段而未将其附加到您的活动。 尝试在HSV中添加FrameLayouts并用片段替换它们。