Andrain Andrain - 22 days ago 6
Android Question

adding a fragments multiple times in linear layout programmatically

I want to add a fragment multiple time in the linear layout. For this purpose I'm using a For loop and passing data to the fragment. Each index has different data. My current problem is this I can only see the last fragment on the screen multipe times probably it is hiding the top fragment or any other reason. Don't understand what's the issue.

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import com.winettv.database.DatabaseHandler;
import com.winettv.livechannels.SubFragment;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private static FragmentManager fragmentManager;//fragment manager to work on fragments
private static ArrayList<DataModel> categoryArrayList;
DatabaseHandler dbHelper;
Context context;
FragmentTransaction ft;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dbHelper = new DatabaseHandler(this);
HearderFragment hfrag = new HearderFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.headercontainer, hfrag).commit();
showChannels();
}

public void showChannels() {

categoryArrayList = dbHelper.getChannelInfo();
fragmentManager =getSupportFragmentManager();
ft = fragmentManager.beginTransaction();
for (int i = 0; i<categoryArrayList.size(); i++) {

SubFragment frag = new SubFragment();
Bundle bundle = new Bundle();
bundle.putInt("ID", (categoryArrayList.get(i).getChannelCategoryId()));
bundle.putString("categoryName", categoryArrayList.get(i).getChannelName());
frag.setArguments(bundle);
ft.add(R.id.container, frag,"fragment"+i);


}
ft.commit();
}
}


Fragment code:

public class SubFragment extends Fragment {

private static View view;
private static RecyclerView gridRecyclerView;
private static GridView_Recycler_Adapter adapter;
private static ArrayList<VideoEntity> gridArrayList;
Context context;
DatabaseHandler dbHelper;
int categoryId;
String categoryName;
TextView tvChannelTitle;


public SubFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.gridlayout_fragment, container, false);

dbHelper = new DatabaseHandler(getActivity());
Bundle bundle = this.getArguments();
categoryId = bundle.getInt("categoryID");
categoryName= bundle.getString("categoryName");
init();
populatRecyclerView();
return view;
}

private void init() {

gridRecyclerView = (RecyclerView) view.findViewById(R.id.grid_recyclerview);
gridRecyclerView.setHasFixedSize(true);// set it fixed size
gridRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));

}

private void populatRecyclerView() {

dbHelper = new DatabaseHandler(getActivity());
tvChannelTitle = (TextView)view.findViewById(R.id.tv);
tvChannelTitle.setText(categoryName);
gridArrayList = dbHelper.getChannels(categoryId);

int listsize = gridArrayList.size();
if(listsize >0){
adapter = new GridView_Recycler_Adapter(getActivity(), gridRecyclerView, gridArrayList, categoryName);
gridRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
else{
Log.e("no data to show","yes");
}
}


}

xml:

<LinearLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="50dp"
android:background="#252424"
android:layout_below="@id/headercontainer"
android:layout_marginLeft="30dp">



Answer

You're using the same Bundle for each Fragment's arguments, and FragmentTransactions happen asynchronously, so when they finally execute, the Fragments are all reading the same arguments.

You presumably have something like the following before the loop:

Bundle bundle = new Bundle();

Move that to inside the for loop, before the put*() calls.

Comments