How to fix fragment on back pressed button distortion? - android

I have a fragment with this layout:
If I click one button I get a list in a Recyclerview, everything is shown ok but on back pressed I have thi button distortion:
Here's the xml of the first fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/category_events_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.FragmentCategoryEvents"
android:orientation="vertical"
android:weightSum="10">
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/textGrid"
android:text="Scegli una categoria"
android:textSize="25sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<GridLayout
android:columnCount="2"
android:rowCount="2"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="14dp">
<!-- Riga 1 -->
<!-- Colonna 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="12dp"
></LinearLayout>
<Button
android:id="#+id/btn_auto"
android:text="Auto"
android:textSize="18sp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<!-- colonna 2 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="12dp"
></LinearLayout>
<Button
android:id="#+id/btn_moto"
android:text="Moto"
android:textSize="18sp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<!-- Riga 2 -->
<!-- Colonna 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="12dp"
></LinearLayout>
<Button
android:id="#+id/btn_corsa"
android:text="Corsa"
android:textSize="18sp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<!-- colonna 2 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp"
></LinearLayout>
<Button
android:id="#+id/btn_bicicletta"
android:text="Bicicletta"
android:textSize="18sp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
</GridLayout>
</LinearLayout>
This is the fragment code:
public class FragmentCategoryEvents extends Fragment {
private Button btn_auto;
private Button btn_moto;
private Button btn_corsa;
private Button btn_bicicletta;
public FragmentCategoryEvents() {
// Required empty public constructor
}
public static FragmentCategoryEvents newInstance() {
FragmentCategoryEvents fragment = new FragmentCategoryEvents();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment_category_events, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
btn_auto = view.findViewById(R.id.btn_auto);
btn_moto = view.findViewById(R.id.btn_moto);
btn_corsa = view.findViewById(R.id.btn_corsa);
btn_bicicletta = view.findViewById(R.id.btn_bicicletta);
btn_auto.setOnClickListener(v -> getCategoryEvents("Auto"));
btn_moto.setOnClickListener(v-> getCategoryEvents("Moto"));
btn_corsa.setOnClickListener(v -> getCategoryEvents("Corsa"));
btn_bicicletta.setOnClickListener(v -> getCategoryEvents("Bicicletta"));
}
private void getCategoryEvents(String category){
FragmentShowCategory newFragment = new FragmentShowCategory();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("category_key",category);
newFragment.setArguments(bundle);
transaction.replace(R.id.category_events_layout,newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
On Button pressed I have this layout:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container_category"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recycler_view_category"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
And the code:
public class FragmentShowCategory extends Fragment {
private RecyclerView mRecyclerView;
private SwipeRefreshLayout mSwipeRefreshLayout;
private CompositeSubscription mCompositeSubscription;
public FragmentShowCategory() {
// Required empty public constructor
}
public static FragmentShowCategory newInstance() {
FragmentShowCategory fragment = new FragmentShowCategory();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_show_category, container, false);
}
#SuppressLint("ResourceAsColor")
#Override
public void onViewCreated (View view, #Nullable Bundle savedInstanceState){
mRecyclerView = view.findViewById(R.id.recycler_view_category);
mRecyclerView.setAdapter(new RecyclerViewAdapter(null));
mRecyclerView.setLayoutManager(null);
mSwipeRefreshLayout = view.findViewById(R.id.swipe_container_category);
Bundle bundle = getArguments();
mCompositeSubscription = new CompositeSubscription();
categoryEventsProcess(bundle.getString("category_key"));
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
categoryEventsProcess(bundle.getString("category_key"));
}
});
}
});
}
public void onBackPressed()
{
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();
}
private void categoryEventsProcess(String category){
mCompositeSubscription.add(NetworkUtil.getRetrofit().getEventsByCategory(category)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(this::handleResponse,this::handleError));
}
private void handleResponse(ArrayList<Evento> eventos) {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(eventos);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(adapter);
mSwipeRefreshLayout.setRefreshing(false);
}
private void handleError(Throwable throwable) {
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
How can I fix this problem?

Solved removing layout_columnWeight and layout_rowWeight attributes and setting layout_width and layout_height in Cardview.

Related

Create Imageview when I click recyclerView item in tabLayout with ViewPager2

First, my app is like this.
I want to add imageview to fragment 1 when I click recyclerview item but imageview doesn't appear
I don't know the problem
Thank you in advance!
I try this :
SpaceEditAdapter.java (recyclerView adapter)
public class SpaceEditAdapter extends RecyclerView.Adapter<SpaceEditAdapter.ItemViewHolder> {
public ArrayList<SpaceEditData> spaceData =new ArrayList<>();
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType ){
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_space_edit_item,parent,false);
}
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, int position){
holder.onBind(spaceData.get(position));
}
#Override
public int getItemCount(){
return spaceData.size();
}
public void addItem(SpaceEditData d){
spaceData.add(d);
}
class ItemViewHolder extends RecyclerView.ViewHolder{
ImageView myItemImageView;
ItemViewHolder(View itemView){
super(itemView);
myItemImageView=itemView.findViewById(R.id.myItemImg);
myItemImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = getAdapterPosition() ;
if (pos != RecyclerView.NO_POSITION) {
SpaceEditData item=spaceData.get(pos);
Bundle bundle=new Bundle();
bundle.putSerializable("num", item);
SpaceFragment sf=new SpaceFragment();
sf.setArguments(bundle);
}
}
});
}
void onBind(SpaceEditData data){
myItemImageView.setImageResource(data.getMyItemImg());
}
}
}
SpaceFragment.java(fragment1 in image)
public class SpaceFragment extends Fragment {
private View v;
private ViewPager2 viewPager;
private TabLayout tabLayout;
private SpaceEditPaintFragment paintF;
private SpaceEditForestFragment forestF;
private SpaceEditSeaFragment seaF;
private SpaceEditMudFragment mudF;
private int count=1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_space, container, false);
createFragment();
createViewPager();
settingTabLayout();
ImageView imageView = new ImageView(v.getContext());
Bundle bundle=getArguments();
if(bundle!=null) {
SpaceEditData data=(SpaceEditData)bundle.getSerializable("num");
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(layoutParams);
imageView.setImageResource(data.getMyItemImg());
spaceLay.addView(imageView);
}
return v;
}
private void createFragment(){
paintF=new SpaceEditPaintFragment();
forestF=new SpaceEditForestFragment();
seaF=new SpaceEditSeaFragment();
mudF=new SpaceEditMudFragment();
}
private void createViewPager(){
viewPager = (ViewPager2)v.findViewById(R.id.spaceEditViewpager);
SpaceEditViewPagerAdapter pagerAdapter=new SpaceEditViewPagerAdapter(getFragmentManager(),getLifecycle());
pagerAdapter.addFragment(paintF);
pagerAdapter.addFragment(forestF);
pagerAdapter.addFragment(seaF);
pagerAdapter.addFragment(mudF);
viewPager.setAdapter(pagerAdapter);
viewPager.setUserInputEnabled(false);
}
private void settingTabLayout(){
tabLayout=(TabLayout)v.findViewById(R.id.spaceEditTabLayout);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab) {
int pos = tab.getPosition();
switch(pos){
case 0:
viewPager.setCurrentItem(0);
break;
case 1:
viewPager.setCurrentItem(1);
break;
case 2:
viewPager.setCurrentItem(2);
break;
case 3:
viewPager.setCurrentItem(3);
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
SpaceEditData.java(RecyclerView item)
import java.io.Serializable;
public class SpaceEditData implements Serializable {
private int myItemImg;
public int getMyItemImg() {
return myItemImg;
}
public void setMyItemImg(int myItemImg) {
this.myItemImg = myItemImg;
}
}
fragment_space.xml(fragment1 in image)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/spaceShopFLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lightBeige"
tools:context=".SpaceFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/spaceLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lightBeige">
<ImageView
android:id="#+id/spaceShop"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="280dp"
android:layout_marginLeft="280dp"
android:layout_marginTop="20dp"
android:clickable="true"
android:contentDescription="#string/imageView"
android:focusable="true"
android:src="#drawable/ic_shop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/spaceEdit"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:contentDescription="#string/imageView"
android:src="#drawable/ic_edit"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="#id/spaceShop"
android:clickable="true"
android:focusable="true" />
<com.example.zeve.StickerView
android:id="#+id/stickerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/spaceEditLay"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="404dp"
android:background="#color/brown">
<com.google.android.material.tabs.TabLayout
android:id="#+id/spaceEditTabLayout"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabGravity="center">
<com.google.android.material.tabs.TabItem
android:id="#+id/editPaintTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/paint"></com.google.android.material.tabs.TabItem>
<com.google.android.material.tabs.TabItem
android:id="#+id/editForestTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/forest"></com.google.android.material.tabs.TabItem>
<com.google.android.material.tabs.TabItem
android:id="#+id/editSeaTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sea"></com.google.android.material.tabs.TabItem>
<com.google.android.material.tabs.TabItem
android:id="#+id/editMudTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mud"></com.google.android.material.tabs.TabItem>
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/spaceEditViewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_marginTop="40dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
fragment_space_edit_item.xml(recyclerView item xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="75dp"
android:layout_height="60dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
<ImageView
android:id="#+id/myItemImg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"/>
android:background="#color/lightBeige"
android:contentDescription="#string/imageView"
</LinearLayout>
It's silly but in your fragment_space_edit_item.xml
<ImageView
android:id="#+id/myItemImg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"/>
android:background="#color/lightBeige"
android:contentDescription="#string/imageView"
background and contentDescription are Error if that is the case.

ViewPager with FragmentPagerAdapter Not showing Content

I am trying to use Fragment
This is my activity class:
public class InterfaceAct extends FragmentActivity implements View.OnClickListener {
private ViewPager mViewPager;
private List<Fragment> datas;
private ViewPagerFragmentAdapter viewPagerFragmentAdapter;
private LinearLayout mLLHome,mLLTimer,mLLEdit,mLLAbout;
private ImageView mImageViewHome,mImageViewTimer,mImageViewEdit,mImageViewAbout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activiy_interface);
initDatas();// Init Data For Fragments
initView();// Init Component
initEvent();// Sign for Click Listener
viewPagerFragmentAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),datas);// Init Adpater Class
mViewPager.setAdapter(viewPagerFragmentAdapter);
}
private void initDatas() {
datas=new ArrayList<Fragment>();
datas.add(new MyFragment1());
datas.add(new MyFragment2());
datas.add(new MyFragment3());
datas.add(new MyFragment4());
}
private void initEvent() {
mLLHome.setOnClickListener(this);
mLLTimer.setOnClickListener(this);
mLLEdit.setOnClickListener(this);
mLLAbout.setOnClickListener(this);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {//ViewPager Scrolling Switch Listner
#Override
public void onPageSelected(int arg0) {
int currentItem=mViewPager.getCurrentItem();
resetImag();
switch (currentItem) {
case 0:
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case 1:
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case 2:
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case 3:
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mLLHome = (LinearLayout) findViewById(R.id.ll_home);
mLLTimer = (LinearLayout) findViewById(R.id.ll_timer);
mLLEdit = (LinearLayout) findViewById(R.id.ll_edit);
mLLAbout = (LinearLayout) findViewById(R.id.ll_about);
mImageViewHome = (ImageView) findViewById(R.id.img_home);
mImageViewTimer = (ImageView) findViewById(R.id.img_timer);
mImageViewEdit = (ImageView) findViewById(R.id.img_edit);
mImageViewAbout = (ImageView) findViewById(R.id.img_about);
}
#Override
public void onClick(View v) {
resetImag();
switch (v.getId()) {
case R.id.ll_home:
mViewPager.setCurrentItem(0);
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case R.id.ll_timer:
mViewPager.setCurrentItem(1);
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case R.id.ll_edit:
mViewPager.setCurrentItem(2);
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case R.id.ll_about:
mViewPager.setCurrentItem(3);
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
private void resetImag() {// Reset Picture
mImageViewHome.setImageResource(R.drawable.home_no);
mImageViewTimer.setImageResource(R.drawable.timer_no);
mImageViewEdit.setImageResource(R.drawable.edit_no);
mImageViewAbout.setImageResource(R.drawable.about_no);
}
}
The ViewPagerFragmentAdapter Looks like this:
public class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> datas;
public ViewPagerFragmentAdapter(FragmentManager fm,List<Fragment> datas) {
super(fm);
this.datas=datas;
}
#Override
public Fragment getItem(int position) {// Return View Object
return datas.get(position);
}
#Override
public int getCount() {// Return View Num
return datas.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {// Init View
return super.instantiateItem(container, position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {// Destroy View
super.destroyItem(container, position, object);
}
}
The layout for the page, it includes a viewpager and also a menu bar in linear layout:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#ffffffff"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/ll_home"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_home"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/home_yes" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_timer"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_timer"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/timer_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timer"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_edit"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_edit"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/edit_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_about"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_about"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/about_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:textColor="#b6b3b3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And This is one of the fragment:
public class MyFragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1,null);
}
}
I have four fragment prepared for viewpager(they are basiclly the same but with differnt layout), here is what the tab1 layout might look like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Chat"
android:textSize="30sp" >
</TextView>
</LinearLayout>
And I only got the menu bar when I run it, the viewpager is not set and I don't know why
Remove android:layout_weight="1" <Viewpager> and add android:orientation="vertical" to Linear layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
></android.support.v4.view.ViewPager>
//-----------

RecyclerView not displaying any data

In my Application I m having a RecyclerView in one of my Fragments in which I am getting response from a server using Reterofit. Every thing is fine but My RecylerView is not displaying any data returned from my service. Even the
int count = offerRecyclerAdapter.getItemCount();
of my adapter is returning the correct amount of data count returned from server.
Inside Fragment:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
offersRecyclerView = (RecyclerView) view.findViewById(R.id.offer_recycler);
MotorCityArabiaGlobal global = (MotorCityArabiaGlobal) getActivity().getApplication();
ApiInterface apiService=global.getClient().create(ApiInterface.class);
Call<OfferResponse> call = apiService.getOffers(4);
call.enqueue(new Callback<OfferResponse>() {
#Override
public void onResponse(Call<OfferResponse> call, Response<OfferResponse> response) {
OfferResponse offers = response.body();
if(offers!=null){
List<Offer> result = response.body().getResult();
offerRecyclerAdapter = new OffersAdapter(getActivity(),result);
offersRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
offersRecyclerView.setItemAnimator(new DefaultItemAnimator());
offersRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
offersRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
int count = offerRecyclerAdapter.getItemCount();
offerRecyclerAdapter.notifyDataSetChanged();
}
}
#Override
public void onFailure(Call<OfferResponse> call, Throwable t) {
}
});
}
Adapter Class:
public class OffersAdapter extends RecyclerView.Adapter<OffersAdapter.MyViewHolder> {
private List<Offer> offersList= Collections.emptyList();
private LayoutInflater inflater;
Context ctx;
public OffersAdapter(Context ctx,List<Offer> offersList) {
this.ctx = ctx;
this.inflater = LayoutInflater.from(ctx);
this.offersList = offersList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// View view = LayoutInflater.from(parent.getContext())
// .inflate(R.layout.offer_list_layout, parent, false);
View view = inflater.inflate(R.layout.offer_list_layout,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Offer offer = offersList.get(position);
holder.title.setText(offer.getOffer_title());
holder.price.setText(offer.getPrice());
holder.offerCount.setText(offer.getOffer_count());
}
#Override
public int getItemCount() {
return offersList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, offerCount, price;
public ImageView image;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
offerCount = (TextView) view.findViewById(R.id.offer_count);
price = (TextView) view.findViewById(R.id.price);
image = (ImageView)view.findViewById(R.id.car_img);
}
}
}
home_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/header_wrapper"
>
<!-- Row1-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!-- Column1 row 1-->
<LinearLayout
android:layout_width="140dp"
android:layout_height="125dp"
android:layout_weight="1"
android:background="#drawable/new_car_selector"
android:clickable="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
></LinearLayout>
<!-- Column2 row 1-->
<LinearLayout
android:layout_width="140dp"
android:layout_height="125dp"
android:layout_weight="1"
android:background="#drawable/find_car_selector"
android:clickable="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
></LinearLayout>
</LinearLayout>
<!-- Row2-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
>
<!-- Column1 row 2-->
<!-- Column2 row 2-->
<LinearLayout
android:layout_width="140dp"
android:layout_height="125dp"
android:layout_weight="1"
android:background="#drawable/sell_car_selector"
android:clickable="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
></LinearLayout>
<LinearLayout
android:layout_width="140dp"
android:layout_height="125dp"
android:layout_weight="1"
android:background="#drawable/compare_car_selector"
android:clickable="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
></LinearLayout>
</LinearLayout>
</LinearLayout>
<!-- Header Wrapper ends-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header_wrapper"
android:id="#+id/offers_recycler_wrapper"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAR OFFERS"
android:textColor="#color/colorAccentDark"
android:textStyle="bold"
android:textSize="16sp"
android:layout_marginLeft="5dp"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/offer_recycler"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
</RelativeLayout>
offer_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical"
android:paddingTop="6dp"
android:paddingBottom="5dp"
>
<ImageView
android:layout_width="80dp"
android:layout_height="60dp"
android:src="#drawable/sell_car_select"
android:id="#+id/car_img"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/meta_data"
android:orientation="vertical"
android:layout_toEndOf="#id/car_img"
android:layout_marginLeft="5dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TITLE"
android:textSize="16dp"
android:textColor="#000"
android:textStyle="bold"
android:id="#+id/title"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="3dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/price"
android:text="13,000"
android:textStyle="bold"
android:textColor="#color/colorAccentDark"
android:textSize="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/curr"
android:textColor="#BDBDBD"
android:text=" SAUDI RAYAL"
android:textSize="12dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/offer_count_wrapper"
android:orientation="horizontal"
android:layout_marginTop="3dp"
>
<ImageView
android:layout_width="12dp"
android:layout_height="12dp"
android:src="#drawable/ic_offer_orange"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:text=" 3 Offers"
android:id="#+id/offer_count"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
add the line below in your onResponse after offersRecyclerView.addItemDecoration();
offersRecyclerView.setAdapter(offerRecyclerAdapter);
hope this helps.
You did not set the adapter to the RecyclerView. Use setAdapter method. See the following code,
List<Offer> results = new ArrayList<Offer>();
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
offersRecyclerView = (RecyclerView) view.findViewById(R.id.offer_recycler);
offersRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
offersRecyclerView.setItemAnimator(new DefaultItemAnimator());
offersRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
offerRecyclerAdapter = new OffersAdapter(getActivity(), results)
offersRecyclerView.setAdapter(offerRecyclerAdapter);
offersRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
MotorCityArabiaGlobal global = (MotorCityArabiaGlobal) getActivity().getApplication();
ApiInterface apiService=global.getClient().create(ApiInterface.class);
Call<OfferResponse> call = apiService.getOffers(4);
call.enqueue(new Callback<OfferResponse>() {
#Override
public void onResponse(Call<OfferResponse> call, Response<OfferResponse> response) {
OfferResponse offers = response.body();
if(offers != null){
List<Offer> result = response.body().getResult();
results.clear();
results.addAll(result);
offerRecyclerAdapter.notifyDataSetChanged();
}
}
#Override
public void onFailure(Call<OfferResponse> call, Throwable t) {
}
});
}

ViewPager in Custom Dialog

I have custom dialog to show which has instruction about how to use the app.I am using ViewPager for this inside my custom dialog layout.
I am getting error
java.lang.IllegalArgumentException: No view found for id for fragment FragmentForInstruction1.
I have created a method which is called in onCreate(Bundle savedInstanceState) {} method .The method inflates the layout for dialog.
private void showCustomDialogForInstruction() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.dialog_layout_for_instruction_message, null, false);
layout.setAlpha(0.2f);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(dialogLayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
ViewPager viewPager=(ViewPager) dialog.findViewById(R.id.pagerInstruction);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentForInstruction1());
adapter.addFrag(new Fragme`enter code here`ntForWelcomePage2());
adapter.addFrag(new FragmentForWelcomePage3());
adapter.addFrag(new FragmentForWelcomePage4());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new OnPageChangeListener()
{
#Override
public void onPageSelected(int pos)
{
}
#Override
public void onPageScrolled(int pos, float arg1, int arg2)
{
}
#Override
public void onPageScrollStateChanged(int arg0)
{
}
});
Button done = (Button) dialog.findViewById(R.id.done);
done.setText("Got It");
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button neverShow = (Button) dialog.findViewById(R.id.nevershow);
neverShow.setText("Never Show Again");
neverShow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
}
my view pager class:
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<Fragment>();
public ViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
#Override
public Fragment getItem(int position)
{
return mFragmentList.get(position);
}
#Override
public int getCount()
{
return mFragmentList.size();
}
public void addFrag(Fragment fragment)
{
mFragmentList.add(fragment);
}
}
and i call the method in MainActivity :
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferencesNeverShowAgain ;
boolean neverShowAgain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferencesNeverShowAgain = PreferenceManager.getDefaultSharedPreferences(this);
neverShowAgain = sharedPreferencesNeverShowAgain.getBoolean("NeverShowAgain", false);
if(!neverShowAgain){
showCustomDialogForMessage();
}
}
}
and the layout for dialog is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dialog_shape"
android:orientation="vertical" >
<!-- layout title -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/top_edge_rounded"
android:flipInterval="2000"
android:padding="20dp" >
<TextView
android:id="#+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/textColor"
android:textStyle="normal" />
<TextView
android:id="#+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/accent"
android:textStyle="normal" />
</ViewFlipper>
<View
android:id="#+id/tri"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/triangle"
android:rotation="180" />
</LinearLayout>
<!-- layout dialog content -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="#+id/pagerInstruction"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/layoutIndicater"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
<!-- layout dialog buttons -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#drawable/all_rounded_edge_plum_for_dialog" >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:background="#color/textColor" />
<View
android:id="#+id/ViewColorPickerHelper"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/textColor" />
<Button
android:id="#+id/nevershow"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Close"
android:textColor="#color/textColor" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Done"
android:textColor="#color/textColor" />
</RelativeLayout>
</LinearLayout>
FragmentForInstruction1 code:
public class FragmentForInstruction1 extends Fragment{
#Override
#Nullable
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragmentinstructionpage1, container,false);
return view;
}
}
and its layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/backgroundColor"
android:gravity="center" >
<TextView
android:id="#+id/titleWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/messageWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="normal"
android:paddingLeft="20dp"
android:paddingRight="20dp"/>
</LinearLayout>
You should create an adapter that extends FragmentPagerAdapter:
private class CustomPagerAdapter extends FragmentPagerAdapter {
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentForInstruction1();
case 1:
return new FragmentForInstruction2();
case 2:
return new FragmentForInstruction3();
case 3:
return new FragmentForInstruction4();
default:
return new FragmentForInstruction1();
}
}
#Override
public int getCount() {
return 4;
}
}
While crating an instance of it, pass the getChildFragmentManager() instead of getSupportFragmentManager():
viewPager.setAdapter(new CustomPagerAdapter(getChildFragmentManager()));

Android: Multi Pane layout detail view appears by default

In my app, I have requirement of Multi Payne layout. In this layout, the first fragment is a ListView which shows the list of items. On click of the list item, a detail view will open up on the right hand side of the list item. But, in my case, when I run my app on tablet, the detail view appears along with ListView by default. While, I want that it should appear on click of the list item.
Below is my code:
Activity Class:
public class OrderActivity extends FragmentActivity implements
OnOrderSelectedListener {
private static final String TAG = "OrderActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate called");
setContentView(R.layout.order_details);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
OrderListFragment orderListFragment = new OrderListFragment();
orderListFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, orderListFragment).commit();
}
}
#Override
public void onOrderSelected(int position) {
Log.d(TAG, "onOrderSelected called");
OrderDetailFragment detailsFrag = (OrderDetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.order_detail_fragment);
if (detailsFrag != null) {
if (!detailsFrag.isVisible()) {
detailsFrag.setUserVisibleHint(true);
detailsFrag.updateOrderView(position);
}
} else {
OrderDetailFragment newFragment = new OrderDetailFragment();
Bundle args = new Bundle();
args.putInt(OrderDetailFragment.ARG_POSITION, position);
newFragment.setArguments(args);;
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
LIst Fragment
public class OrderListFragment extends ListFragment {
private static final String TAG = "OrderListFragment";
OnOrderSelectedListener mOnOrderSelectedListener;
public interface OnOrderSelectedListener {
public void onOrderSelected(int position);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate called");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView called");
return inflater.inflate(R.layout.order_list, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated called");
ArrayList<Data> mDataList = new ArrayList<Data>();
Data mData1 = new Data("1", "11001", "08/07/2013", "GAUGE_RUN",
"Dispatched", "Terminal:", "Rail Terminal:", "New York",
"Washington DC");
Data mData2 = new Data("1", "11002", "08/07/2013", "GAUGE_RUN",
"Dispatched", "Terminal:", "Rail Terminal:", "New York",
"Washington DC");
mDataList.add(mData1);
mDataList.add(mData2);
setListAdapter(new OrderAdapter(getActivity(),
R.layout.order_list_item, mDataList));
}
#Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart called");
if (getFragmentManager().findFragmentById(R.id.order_detail_fragment) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(TAG, "onAttach called");
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mOnOrderSelectedListener = (OnOrderSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d(TAG, "onListItemClicked");
mOnOrderSelectedListener.onOrderSelected(position);
getListView().setItemChecked(position, true);
}
}
Detail Fragment
public class OrderDetailFragment extends Fragment {
public final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
private static final String TAG = "OrderDetailFragment";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView called");
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
return inflater.inflate(R.layout.acceptance_details, container, false);
}
#Override
public void onStart() {
Log.d(TAG, "onStart called");
super.onStart();
Bundle args = getArguments();
if (args != null) {
updateOrderView(args.getInt(ARG_POSITION));
} else if (mCurrentPosition != -1) {
updateOrderView(mCurrentPosition);
}
}
public void updateOrderView(int position) {
Log.d(TAG, "updateOrderView called");
/*
* TextView article = (TextView)
* getActivity().findViewById(R.id.article);
* article.setText(Ipsum.Articles[position]); btnNext = (Button)
* getActivity().findViewById(R.id.btnNext);
* btnNext.setOnClickListener(this); mCurrentPosition = position;
*/
}
#Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState called");
super.onSaveInstanceState(outState);
outState.putInt(ARG_POSITION, mCurrentPosition);
}
}
Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="horizontal" >
<com.dzo.dispatchcrude.driverapp.ui.HeaderBar
android:id="#+id/headerBar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true" >
</com.dzo.dispatchcrude.driverapp.ui.HeaderBar>
<LinearLayout
android:id="#+id/linOrderView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/headerBar"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="#+id/order_list_fragment"
android:name="com.dzo.dispatchcrude.driverapp.ui.OrderListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/order_detail_fragment"
android:name="com.dzo.dispatchcrude.driverapp.ui.OrderDetailFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="30dp"
android:layout_weight="2" />
</LinearLayout>
</RelativeLayout>
List Fragment layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />
</LinearLayout>
Order Detail Fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#drawable/corner_shape"
android:orientation="vertical" >
<TextView
android:id="#+id/txtHeaderType"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="-5dp"
android:background="#drawable/upper_corner"
android:gravity="center"
android:text="#string/acceptance_details"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtHeaderSource"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#drawable/rectangle"
android:gravity="center"
android:textSize="25sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTruck"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/truck"
android:textColor="#color/text_color"
android:textSize="20sp"
android:typeface="sans" />
<Spinner
android:id="#+id/truckSpinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTrailor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/trailor"
android:textColor="#color/text_color"
android:textSize="20sp" />
<Spinner
android:id="#+id/trailorSpinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTrailor2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/trailor2"
android:textColor="#color/text_color"
android:textSize="20sp" />
<Spinner
android:id="#+id/trailor2Spinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<Button
android:id="#+id/btnAccept"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="25dp"
android:background="#drawable/input_button"
android:gravity="center"
android:text="#string/accept"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
I did gone through Fragments Basic Demo given on the Android developer's site, but couldn't figure out my mistake.
try this
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listview.setItemChecked(0,false);

Categories

Resources