Answer the question
In order to leave comments, you need to log in
How to add Fragment to LinearLayout?
I want to add a video to my Youtube news app. I use LinearLayout to mark up the news, in which
pictures and text are parsed from the site. There were problems adding the video. The problem is that there can be several videos in one news. I found only this example, which is suitable for one video
@Override
protected void onPostExecute(Spanned spanned) {
super.onPostExecute(spanned);
title.setText(strTitle);
textView.setText(spanned);
VideoFragment f = VideoFragment.newInstance(VIDEO_ID);
FrameLayout frameLayout = new FrameLayout(NewsContent.this);
frameLayout.setId(R.id.fragment1);
linearLayout.addView(frameLayout);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, f).commit();
progressDialog.dismiss();
}
}
Answer the question
In order to leave comments, you need to log in
The most banal is this:
@Override
protected void onPostExecute(Spanned spanned) {
super.onPostExecute(spanned);
title.setText(strTitle);
textView.setText(spanned);
for (List<String> id : ids) {
VideoFragment f = VideoFragment.newInstance(id);
FrameLayout frameLayout = new FrameLayout(NewsContent.this);
frameLayout.setTag(id);
linearLayout.addView(frameLayout);
getSupportFragmentManager().beginTransaction().replace(frameLayout.getId(), f).commit();
}
progressDialog.dismiss();
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/scrollContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</ScrollView>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
String[] ids = new String[]{"1", "2", "3", "4", "5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.scrollContainer);
int position = 0;
for (String id : ids) {
FrameLayout fm = new FrameLayout(this);
fm.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500));
linearLayout.addView(fm);
fm.setId(++position);
BlankFragment fragment = BlankFragment.newInstance(id);
getFragmentManager().beginTransaction().replace(position, fragment, id).commit();
}
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
public class BlankFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private String mParam1;
private TextView title;
public static BlankFragment newInstance(String param1) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
public BlankFragment() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_blank, container, false);
title = (TextView) v.findViewById(R.id.title);
title.setText(mParam1);
return v;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question