how to set fragment in activity and add cardview in recyclerview - android

I am trying to build a e-commerce app, now I am stucked here. When try to add fragment class in my activity some errors showing.
when I open my fragment recyclerview items bottom margin is too long, its adjusting automatically.
when I back to activity fragment class items and activity items all showing
when I back a blank page is shown
my codes are below
//this is my activity.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<FrameLayout
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
In this activity.xml had a frame layout.i try to add fragment in this frame layout. and i have two fragnent class.fragment one and two
This is my one_fragment.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/app_bar"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:layout_margin="16dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/old_order_recycle"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
//two_fragment.xml
<android.support.v4.widget.NestedScrollView
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sirajmalayil.sneakers.OrderActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
app:titleTextAppearance="#style/Toolbar.TitleText"
android:minHeight="?attr/actionBarSize"
android:background="#color/colorPrimaryDark"/>
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_below="#+id/toolbar"
android:background="#color/colorWhite">
<TextView
android:text="no active orders"
android:textAllCaps="true"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:textSize="14sp"
android:fontFamily="sans-serif"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="There are no recent orders to show."
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:fontFamily="sans-serif"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#mipmap/footprints"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:id="#+id/txt_start_shopping"
android:text="start shopping"
android:background="#drawable/login_btn"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_gravity="center"
android:textAllCaps="true"
android:textColor="#color/colorPink"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="#+id/txt_old_orders"
android:text="show my old orders"
android:gravity="center"
android:padding="10dp"
android:textAllCaps="true"
android:layout_margin="16dp"
android:textColor="#color/colorPink"
android:textStyle="bold"
android:background="#drawable/rectangle_colored_bg"
android:layout_below="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
//this is my two_fragment class
public class two_Fragment extends Fragment {
private TextView txtStartShoppi,txtoldOrders;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_active_orders,container,false);
txtStartShoppi = (TextView) rootView.findViewById(R.id.txt_start_shopping);
txtoldOrders = (TextView) rootView.findViewById(R.id.txt_old_orders);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
Drawable drawable = ContextCompat.getDrawable(getActivity(),R.drawable.ic_close_black_24dp);
toolbar.setNavigationIcon(drawable);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
actionBar.setTitle("ORDERS");
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
clickListeners();
return rootView;
}
private void clickListeners() {
txtStartShoppi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),MainActivity.class));
}
});
txtoldOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new OldOrderFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_frame,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}
//this is my activity class
public class OrderActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
if (savedInstanceState == null) {
Fragment fragment = new ActiveOrdersFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_frame, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
//this is my one_fragment
public class one_fragment extends Fragment {
RecyclerView recyclerView;
private List<Orders> ordersList = new ArrayList<>();
private List<products> productList = new ArrayList<>();
OldOrdersAdapter oldOrdersAdapter;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_old_orders,container,false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
actionBar.setTitle("MY ARCHIVED ORDERS");
toolbar.setTitleTextColor(Color.WHITE);
actionBar.setDisplayHomeAsUpEnabled(true);
prepareOrderList();
recyclerView = (RecyclerView) rootView.findViewById(R.id.old_order_recycle);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
oldOrdersAdapter = new OldOrdersAdapter(getActivity(),ordersList);
recyclerView.setAdapter(oldOrdersAdapter);
return rootView;
}
private void prepareOrderList() {
Orders orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
//oldOrdersAdapter.notifyDataSetChanged();
}
}
//this is my Adapter class
public class OldOrdersAdapter extends RecyclerView.Adapter<OldOrdersAdapter.MyViewHolder> {
Context context;
private List<Orders> ordersList = new ArrayList<>();
public OldOrdersAdapter(Context context, List<Orders> ordersList) {
this.context = context;
this.ordersList = ordersList;
}
#NonNull
#Override
public OldOrdersAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_old_orders,parent,false);
return new MyViewHolder(rootView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Orders orders = ordersList.get(position);
holder.orderStatus.setText(orders.getOrder_status());
holder.orderNo.setText(orders.getOrder_no());
holder.orderPlacedDate.setText(orders.getOrder_placed_date());
holder.orderdItemPrice.setText(orders.getOrder_item_price());
holder.orderDeliverDate.setText(orders.getOrder_delivered_date());
}
#Override
public int getItemCount() {
return ordersList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView orderStatus,orderNo,orderPlacedDate,orderdItemPrice,orderDeliverDate;
public MyViewHolder(View itemView) {
super(itemView);
orderStatus = (TextView) itemView.findViewById(R.id.txt_order_status);
orderNo = (TextView) itemView.findViewById(R.id.txt_order_no);
orderPlacedDate = (TextView) itemView.findViewById(R.id.txt_order_placed_date);
orderdItemPrice = (TextView) itemView.findViewById(R.id.txt_order_price);
orderDeliverDate = (TextView) itemView.findViewById(R.id.txt_order_item_deliver_date);
}
}
}
Ok, all codes here.iam new in android . so i dont know whtais the problrm here. some time fragment and activity contents are mixing..
the problems are only in xml file.not in programattic code

Try this code..
set both fragment background color into root attribute it means Relative layout tag.
android:background="#android:color/white"
and card view refer this link it give good demo for that..
https://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/
add fragment into activity then call onCreate method in below code..
Fragment fragment = new MainFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();

Related

Master/Detail layout not rendering

I'm trying to make an app that fetches a book list from an API and displays it on the master list and, upon clicking, it displays the details. It works fine with mobiles, but I can't get it to work with tablets and since there's no error I can't find out where I went wrong.
On tablets it renders as if on a phone instead of rendering the two pane view.
I'm using a single activity with fragments.
"Main" activity:
public class ItemListActivity extends AppCompatActivity {
public static FragmentManager fragmentManager;
private boolean isTwoPane = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
determinePaneLayout();
fragmentManager = getSupportFragmentManager();
if(isTwoPane){
//fragmentManager.beginTransaction().add(R.id.master_dual, new ItemsListFragment()).commit();
fragmentManager.beginTransaction().add(R.id.flDetailContainer, new ItemDetailFragment()).commit();
fragmentManager.beginTransaction().add(R.id.fragmentContainer, new ItemsListFragment()).commit();
} else {
fragmentManager.beginTransaction().add(R.id.fragmentContainer, new ItemsListFragment()).commit();
}
}
private void determinePaneLayout() {
FrameLayout fragmentItemDetail = (FrameLayout) findViewById(R.id.flDetailContainer);
// If there is a second pane for details
if (fragmentItemDetail != null) {
isTwoPane = true;
}
}
Item List Fragment:
public class ItemsListFragment extends Fragment {
private ArrayList<Book> bookList;
private ArrayList<String> bookNames;
private ArrayAdapter<Book> bookArrayAdapter;
private ArrayAdapter<String> bookNamesAdapter;
private ApiInterface apiInterface;
private ListView lvItems;
private static final String bookKey = "newBook";
private static Book nBook;
public ItemsListFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_items_list,
container, false);
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lvItems = view.findViewById(R.id.lvItems);
bookNames = new ArrayList<>();
//bookNames.add(0, "Gabriel");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<ArrayList<Book>> call = apiInterface.getBooks();
call.enqueue(new Callback<ArrayList<Book>>() {
#Override
public void onResponse(Call<ArrayList<Book>> call, Response<ArrayList<Book>> response) {
bookList = new ArrayList<>();
bookList = response.body();
bookArrayAdapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_list_item_activated_1, bookList);
lvItems.setAdapter(bookArrayAdapter);
}
#Override
public void onFailure(Call<ArrayList<Book>> call, Throwable t) {
}
});
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
nBook = bookList.get(i);
if(view.findViewById(R.id.flDetailContainer) != null){
ItemListActivity.fragmentManager.beginTransaction().replace(R.id.flDetailContainer, ItemDetailFragment.newInstance(nBook)).addToBackStack(null).commit();
} else {
ItemListActivity.fragmentManager.beginTransaction().replace(R.id.fragmentContainer, ItemDetailFragment.newInstance(nBook)).addToBackStack(null).commit();
}
}
});
}`
Item Detail Fragment:
public class ItemDetailFragment extends Fragment {
private static final String bookKey = "newBook";
private static Book nBook;
private TextView title;
private TextView isbn;
private TextView currency;
private TextView price;
private TextView author;
public static ItemDetailFragment newInstance(Book book) {
ItemDetailFragment fragment = new ItemDetailFragment();
Bundle args = new Bundle();
args.putSerializable(bookKey, book);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
nBook = (Book)getArguments().getSerializable(bookKey);
Log.v("BundleOK", "BundleOK");
} else {
Log.v("Bundle Nulo", "Bundle nulo");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate view
View view = inflater.inflate(R.layout.fragment_item_detail,
container, false);
// Return view
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
title = view.findViewById(R.id.tvTitle);
isbn = view.findViewById(R.id.tvIsbn);
currency = view.findViewById(R.id.tvCurrency);
price = view.findViewById(R.id.tvPrice);
author = view.findViewById(R.id.tvAuthor);
title.setText(nBook.getTitle());
isbn.setText("ISBN: " + nBook.getIsbn());
currency.setText("Currency: "+ nBook.getCurrency());
price.setText("Price: "+ String.valueOf((long)nBook.getPrice()/100));
author.setText("Autor: "+nBook.getAuthor());
}
XML for dual pane:
<?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:id="#+id/LinearLayout1"
android:showDividers="middle"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/fragmentItemsList"
android:name="gabrielgomes.studio.com.itemretrieve.ItemsListFragment"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
tools:layout="#layout/fragment_items_list" />
<View android:background="#000000"
android:layout_width="1dp"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="#+id/flDetailContainer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
XML for the Main Activity (Item List Activity)
<?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="horizontal" >
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
XML for the Item Detail Fragment:
<?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" >
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/baskervillebt"
android:gravity="center"
android:text="Item Title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvIsbn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:fontFamily="#font/baskervilleitalicbt"
android:text="Item Body"
android:textSize="16sp" />
<TextView
android:id="#+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvIsbn"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:fontFamily="#font/baskervilleitalicbt"
android:text="Price"
android:textSize="16sp" />
<TextView
android:id="#+id/tvCurrency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvPrice"
android:layout_centerHorizontal="true"
android:fontFamily="#font/baskervilleitalicbt"
android:layout_marginTop="44dp"
android:text="Currency"
android:textSize="16sp" />
<TextView
android:id="#+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvCurrency"
android:fontFamily="#font/baskervilleitalicbt"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:text="Author"
android:textSize="16sp" />
</RelativeLayout>
I've been cracking my head for 5 days now, so any help is really appreciated! I tried a number of tutorials and searched SO thoroughly but no go!
Cheers!
I believe you have two copies activity_item_list.xml layout file and one is res/layout/ folder and another one is in res/layout-sw600dp/ folder.
sw600dp --> Screen width above 600dp use this layout else if less than 600dp use default layout file.
And also you make sure keep same attribute ids same in both the layout files.

Android viewpager with recyclerview not working in fragment

I am working with recyclerview with viewpager in fragment.
I want to make navigation on bottom of screen.
enter image description here
and when I click each button, each fragment show in the frameLayout.
the problem is happen when I enter into the artist fragment second time.(at first time it show up find as I intended.)
as you can see in this image enter image description here
there is noting shows up in the fragment. and swiping the screen not working properly. It should moving stick to the left or right but it stop at the point where I stop moving my finger.
here is my source.
activity_main_board.xml
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/navigation"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
</FrameLayout>
<LinearLayout
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="7dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
app:srcCompat="#drawable/image_background7" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="53dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/my_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView10"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_my" />
<TextView
android:id="#+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/artwork_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView13"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_artwork" />
<TextView
android:id="#+id/textView35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artwork"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/artist_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView14"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_artist" />
<TextView
android:id="#+id/textView36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artist"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/alarm_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView20"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_alert" />
<TextView
android:id="#+id/textView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarm"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/menu_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView19"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_menu" />
<TextView
android:id="#+id/textView38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전체"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
MainBoradActivity.java
public class MainBoardActivity extends AppCompatActivity implements View.OnClickListener{
private static LinearLayout my_board_button;
private static LinearLayout artwork_board_button;
private static LinearLayout artist_board_button;
private static LinearLayout menu_board_button;
private static LinearLayout alarm_board_button;
private static FrameLayout container;
private static MyBoardFragment myBoardFragment;
private static ArtworkBoardFragment artworkBoardFragment;
private static ArtistBoardFragment artistBoardFragment;
private static AlarmBoardFragment alarmBoardFragment;
private static MenuBoardFragment menuBoardFragment;
private static FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_board);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
container = (FrameLayout) findViewById(R.id.container);
my_board_button = (LinearLayout) findViewById(R.id.my_board_button);
artwork_board_button = (LinearLayout) findViewById(R.id.artwork_board_button);
artist_board_button = (LinearLayout) findViewById(R.id.artist_board_button);
menu_board_button = (LinearLayout) findViewById(R.id.menu_board_button);
alarm_board_button = (LinearLayout) findViewById(R.id.alarm_board_button);
my_board_button.setOnClickListener(this);
artwork_board_button.setOnClickListener(this);
artist_board_button.setOnClickListener(this);
menu_board_button.setOnClickListener(this);
alarm_board_button.setOnClickListener(this);
myBoardFragment = new MyBoardFragment();
artworkBoardFragment = new ArtworkBoardFragment();
artistBoardFragment = new ArtistBoardFragment();
alarmBoardFragment = new AlarmBoardFragment();
menuBoardFragment = new MenuBoardFragment();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.container, myBoardFragment).commitAllowingStateLoss();
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.my_board_button:
FragmentTransaction myTransaction = fragmentManager.beginTransaction();
myTransaction.replace(R.id.container, myBoardFragment);
myTransaction.addToBackStack("0");
myTransaction.commitAllowingStateLoss();
break;
case R.id.artwork_board_button:
FragmentTransaction artworkTransaction = fragmentManager.beginTransaction();
artworkTransaction.replace(R.id.container, artworkBoardFragment);
artworkTransaction.addToBackStack("1");
artworkTransaction.commitAllowingStateLoss();
break;
case R.id.artist_board_button:
FragmentTransaction artistTransaction = fragmentManager.beginTransaction();
artistTransaction.replace(R.id.container, artistBoardFragment);
artistTransaction.addToBackStack("2");
artistTransaction.commitAllowingStateLoss();
break;
case R.id.alarm_board_button:
FragmentTransaction alarmTransaction = fragmentManager.beginTransaction();
alarmTransaction.replace(R.id.container, alarmBoardFragment);
alarmTransaction.addToBackStack("3");
alarmTransaction.commitAllowingStateLoss();
break;
case R.id.menu_board_button:
FragmentTransaction menuTransaction = fragmentManager.beginTransaction();
menuTransaction.replace(R.id.container, menuBoardFragment);
menuTransaction.addToBackStack("4");
menuTransaction.commitAllowingStateLoss();
break;
}
}
}
ArtistBoardFragment.java
public class ArtistBoardFragment extends Fragment {
private static TabLayout artist_tabs;
private static ViewPager artist_container;
private static FragmentPagerAdapter pageAdapter;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_artist_board, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
artist_tabs = (TabLayout) view.findViewById(R.id.artist_tabs);
artist_container = (ViewPager) view.findViewById(R.id.artist_container);
pageAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
ArtistNewFragment artistNewFragment = new ArtistNewFragment();
ArtistCategoryFragment artistCategoryFragment = new ArtistCategoryFragment();
private final String[] menuFragmentNames = new String[]{
"new",
"category"
};
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Bundle recentBundle = new Bundle();
recentBundle.putInt("page", position);
artistNewFragment.setArguments(recentBundle);
return artistNewFragment;
case 1:
Bundle bestBundle = new Bundle();
bestBundle.putInt("page", position);
artistCategoryFragment.setArguments(bestBundle);
return artistCategoryFragment;
default:
return null;
}
}
#Override
public int getCount() {
return menuFragmentNames.length;
}
#Override
public CharSequence getPageTitle(int position) {
return menuFragmentNames[position];
}
};
artist_container.setAdapter(pageAdapter);
artist_container.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
artist_tabs.setupWithViewPager(artist_container);
}
#Override
public void onResume() {
super.onResume();
}
}
ArtistNewFragment.java
public class ArtistNewFragment extends Fragment {
private static RecyclerView new_content_list;
int pastVisibleItems, visibleItemCount, totalItemCount;
private static TimelineAdapter timelineAdapter;
private static RequestManager requestManager;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_artist_new_content, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new_content_list = (RecyclerView) view.findViewById(R.id.new_content_list);
requestManager = Glide.with(getActivity());
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);
new_content_list.setLayoutManager(layoutManager);
new_content_list.setHasFixedSize(true);
new_content_list.setNestedScrollingEnabled(false);
//final NotiAdapter notiAdapter = new NotiAdapter(getApplicationContext(), FunctionBase.createFilter, false, lastCheckTime);
timelineAdapter = new TimelineAdapter(getActivity(), requestManager);
timelineAdapter.setObjectsPerPage(3);
new_content_list.setAdapter(timelineAdapter);
new_content_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.d("dx", String.valueOf(dx));
Log.d("dy", String.valueOf(dy));
if(dy > 0) {
visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisibleItems = layoutManager.findFirstVisibleItemPosition();
if ( (visibleItemCount + pastVisibleItems) >= totalItemCount) {
timelineAdapter.loadNextPage();
}
}
}
});
}
#Override
public void onResume() {
super.onResume();
timelineAdapter.loadObjects(0);
}
}
It's hard to tell exactly due to the navigational structure of your app, but think the solution to your problem is to use the childFragmentManager inside of the ArtistBoardFragment. i.e, instead of
pageAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) ...
Use
pageAdapter = new FragmentPagerAdapter(getChildFragmentManager()) ...

Progress Bar not showing in fragment [ANDROID]

In my app i am using this library
https://github.com/DmitryMalkovich/circular-with-floating-action-button
to implement progress bar with floating action button. Its working on activity but when i included this layout in my fragment's layout progress bar doesn't show.
Here is my code for better explanation
Please guide me where i am going wrong
Any help will be appreciated.
customlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
>
<com.dmitrymalkovich.android.ProgressFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:clickable="true">
<ProgressBar
android:id="#+id/progressbar"
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/transparent_logo"
app:backgroundTint="#color/bg_color"
android:layout_centerInParent="true"
/>
</com.dmitrymalkovich.android.ProgressFloatingActionButton>
</RelativeLayout>
fragmentlayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="bg_color"
>
<include
layout="#layout/customlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="#layout/otherlayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
I am hiding and showing the parent layout of customlayout.xml on runtime when i get data from serivice, before service call i SHOW it with View.VISIBLE and after service call i HIDE it with View.GONE
UPDATED
Java code
Fragment
public class MyFragment extends Fragment {
private Context context;
//root view of layout
View rootView;
private static final String ARG_PARAM1 = "Class";
private String screen_title;
private String URL = "";
public MyFragment() {
// Required empty public constructor
}
public static MyFragment newInstance(String param1) {
MyFragmentfragment = new MyFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
if (getArguments() != null) {
screen_title = getArguments().getString(ARG_PARAM1);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragmentlayout, container, false);
//find views by ids
getData();
return rootView;
}
private void getData() {
CustomClass.getInstance(context, rootView).show();
//service call
//on getting response from serivce (have implemented a listener here)
CustomClass.getInstance(context, rootView).hide(); }
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
CustomClass
public class CustomClass {
private ProgressBar mProgressBar;
private FloatingActionButton mFab;
private RelativeLayout parentLayout;
private LayoutInflater inflater;
private static CustomClass custom;
private CustomClass (Context context) {
findViewsById(context);
}
private CustomClass (View view) {
findViewsById(view);
}
public static CustomClass getInstance(Context context, View view) {
custom = new CustomClass (view);
custom.setPColor(context);
return custom;
}
public static CustomClass getInstance(Context context) {
custom = new CustomClass (context);
custom.setPColor(context);
return custom;
}
private void setPColor(Context context) {
if (mProgressBar != null)
mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(context, R.color.bluecolor), android.graphics.PorterDuff.Mode.MULTIPLY);
}
private void findViewsById(View view) {
if (view != null) {
mProgressBar = (ProgressBar) view.findViewById(R.id.custom_progressbar);
mFab = (FloatingActionButton) view.findViewById(R.id.custom_floatingActionButton);
parentLayout = (RelativeLayout) view.findViewById(R.id.rl_progress_fab_container);
}
}
private void findViewsById(Context context) {
Activity activity = (Activity) context;
if (activity != null) {
mProgressBar = (ProgressBar) activity.findViewById(R.id.custom_progressbar);
mFab = (FloatingActionButton) activity.findViewById(R.id.custom_floatingActionButton);
parentLayout = (RelativeLayout) activity.findViewById(R.id.rl_progress_fab_container);
}
}
public void show() {
if (custom.parentLayout != null)
custom.parentLayout.setVisibility(View.VISIBLE);
}
public void hide() {
if (custom.parentLayout != null)
custom.parentLayout.setVisibility(View.GONE);
}
make your customlayout at the bottom with the parent layout.
so your z index for that layout is at the top and it will be visible and gone that can be seen by you.
You need to use CoordinatorLayout with FAB .
Just put your FAB inside CoordinatorLayout.
As you can see library sample code suggests to use it too.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dmitrymalkovich.android.progressfabsample.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<com.dmitrymalkovich.android.ProgressFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:clickable="true"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_backup_black_24dp"
app:backgroundTint="#color/colorFab" />
<ProgressBar
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.dmitrymalkovich.android.ProgressFloatingActionButton>
<com.dmitrymalkovich.android.ProgressFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:clickable="true">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_backup_black_24dp"
app:backgroundTint="#color/colorFab" />
<ProgressBar
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.dmitrymalkovich.android.ProgressFloatingActionButton>
try this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragmentlayout, container, false);
//find views by ids
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
getData();
}
});
return rootView;
}
or
private void getData() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
CustomClass.getInstance(context, rootView).show();
CustomClass.getInstance(context, rootView).hide();
}
});
}

In a fragment implementation, the ListView below EditText disappears after setting keypad element (of EditText view) to hide

this is image while useing keypad
and this image after keypad was hidden
I use EditText but below it there is ListView. After typing text inputs in EditText and then press done or click back to hide keypad. Then the part of ListView behind the keypad disappeared and replaced by white area (cleared), why ?
NOTE: this fragment is under tablayout and viewpager and the fragment which contain the edit text and listview is launched from the main fragment
this is main fragment
public class FriendsFragment extends Fragment {
public FriendsFragment() {
// Required empty public constructor
}
private FragmentActivity myContext;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview=inflater.inflate(R.layout.friends_fragment, container, false);
viewPager = (ViewPager) rootview.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootview.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
if (tab.getPosition() == 1) {
FindFriendsFragment.myfriends_list.invalidate(FindFriendsFragment.myfriends_list.getLeft(), FindFriendsFragment.myfriends_list.getTop(), FindFriendsFragment.myfriends_list.getRight(), FindFriendsFragment.myfriends_list.getBottom());
FindFriendsFragment.adapter.notifyDataSetChanged();
FindFriendsFragment.myfriends_list.clearFocus();
FindFriendsFragment.myfriends_list.postInvalidate();
}
}
});
TextView friends = (TextView) rootview.findViewById(R.id.search);
Typeface Exo_thin = Typeface.createFromAsset(myContext.getAssets(), "fonts/Exo2.0-Thin.otf");
friends.setTypeface(Exo_thin);
return rootview;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new MyFriendsFragment(), "My Friends");
adapter.addFragment(new FindFriendsFragment(), "Find Friends");
adapter.addFragment(new TwoFragment(), "Friend Requests");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
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 addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
this is xml of main fragment
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/rel"
android:layout_alignParentTop="true"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/search"
android:text="Friends"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:textSize="25sp"
android:textColor="#color/colorVeryDarkBlue"
/>
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="#android:style/TextAppearance.Widget.TabWidget"
app:tabSelectedTextColor="#color/colorLightGreen"
app:tabTextColor="#color/colorDarkGreen"
app:tabMode="scrollable"
android:background="#color/colorDarkBlue"
app:tabIndicatorColor="#color/colorDarkBlue"
app:tabGravity="center"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
the fragment which contain listview and edit text
public class FindFriendsFragment extends Fragment {
public FindFriendsFragment()
{
// Required empty public constructor
}
ListView myfriends_list;
FindFriendsAdapter adapter;
ArrayList<FindFriends> arraylist ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview= inflater.inflate(R.layout.find_friends_fragment, container, false);
EditText search=(EditText) rootview.findViewById(R.id.search);
Typeface Exo_Regular =
Typeface.createFromAsset(getActivity().getAssets(), "fonts/Exo2.0-
Regular.otf");
search.setTypeface(Exo_Regular);
arraylist = new ArrayList<FindFriends>();
arraylist.add(new FindFriends("mina fared", "hello
guys",1,"sdsdsdsds",true )) ;
adapter = new FindFriendsAdapter(getActivity(), arraylist);
myfriends_list.setAdapter(adapter);
adapter.notifyDataSetChanged();
return rootview;
}
}
And the related xml file is of the fragment which contain listview and edittext:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#ffffff"
>
<LinearLayout
android:layout_height="60dp"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_marginTop="111dp"
android:id="#+id/refl"
android:background="#color/colorLightGrey"
android:layout_alignParentTop="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/search"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:background="#ffffff"
android:singleLine="true"
android:hint="Search"
android:ellipsize="start"
android:imeOptions="actionDone"
android:gravity="center_vertical|center_horizontal"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:textColorHint="#color/colormyhintfindfiernds"
android:textColor="#color/colorDarkBlue"
/>
</LinearLayout>
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_marginTop="1dp"
android:layout_below="#+id/refl"
android:divider="#ffffff"
android:dividerHeight="1.5dp"
/>
</RelativeLayout>
this is the parent fragment which contains all fragments
public class ProfileFragment extends Fragment {
public ProfileFragment()
{
// Required empty public constructor
}
RelativeLayout rl1;
DrawView drawView ;
DrawView drawView2;
TextView myprofile,username,notification_txt,colloection_txt,friends_txt,setting_txt,public_profile_txt;
ImageView public_profile_btn,friends;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootview=inflater.inflate(R.layout.myprofile_fragment, container,
false);
username= (TextView) rootview.findViewById(R.id.user_name_txt);
myprofile= (TextView) rootview.findViewById(R.id.myprofile);
notification_txt= (TextView) rootview.findViewById(R.id.notification_txt);
colloection_txt= (TextView) rootview.findViewById(R.id.colloection_txt);
friends_txt= (TextView) rootview.findViewById(R.id.friends_txt);
setting_txt= (TextView) rootview.findViewById(R.id.setting_txt);
public_profile_txt= (TextView) rootview.findViewById(R.id.public_profile_txt);
Typeface Exo_thin = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Exo2.0-Thin.otf");
Typeface Exo_SemiBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Exo2.0-SemiBold.otf");
myprofile.setTypeface(Exo_thin);
username.setTypeface(Exo_SemiBold);
notification_txt.setTypeface(Exo_SemiBold);
colloection_txt.setTypeface(Exo_SemiBold);
friends_txt.setTypeface(Exo_SemiBold);
setting_txt.setTypeface(Exo_SemiBold);
public_profile_txt.setTypeface(Exo_SemiBold);
ImageView x=(ImageView)rootview.findViewById(R.id.colloection);
ImageView y=(ImageView)rootview.findViewById(R.id.friends);
ImageView x1=(ImageView)rootview.findViewById(R.id.setting);
rl1 =(RelativeLayout)rootview.findViewById(R.id.rel2);
final View fragmentContainer = rootview.findViewById(R.id.container);
friends =(ImageView)rootview.findViewById(R.id.friends);
public_profile_btn=(ImageView)rootview.findViewById(R.id.public_profile);
public_profile_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Fragment newFragment = new MyProfileFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(fragmentContainer.getId(), newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
friends.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Fragment newFragment = new FriendsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(fragmentContainer.getId(), newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return rootview;
}
}
from manifest.xml search for your activity tag like this
<activity
android:name=".YourActivity"
android:label="#string/title_activity"
...>
</activity>
then add android:configChanges="keyboard|keyboardHidden" like that
<activity
android:name=".YourActivity"
android:configChanges="keyboard|keyboardHidden"
android:label="#string/title_activity"
...>
</activity>
EDIT
i found that my answer not completely right you may add
android:windowSoftInputMode="adjustNothing"
sorry about that

Why do I can slide to more than two times while I have only two tabs on Android?

I am learning how to use navigation drawer and navigation tab. I created an app that implement a navigation drawer on MainActivity and each item on navigation drawer will replace fragment on MainActivity to the corresponding fragment.
My first fragment provide two navigation tab using SlidingTabLayout and SlidingTabStrip. The other fragments are just a textview.
If I move from the first item to the second item and then back to first item again, my tab still show two tab, but in fact, I can slide more than two times and the content is not shown.
How do I fix this?
What it should be:
After I move to second drawer and then back to first drawer:
What happened after that:
You can see that I can slide more than twice and the content of fragments are not shown.
MainActivity.java
public class MainActivity extends ActionBarActivity {
String titles[] = {"TabsFragment", "TextFragment"};
Fragment fragment[] = {TabsFragment.newInstance(), TextFragment.newInstance()};
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager mLayoutManager;
DrawerLayout drawer;
ActionBarDrawerToggle mDrawerToggle;
static View.OnClickListener drawerItemClickListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
mRecyclerView.setHasFixedSize(true);
mAdapter = new DrawerAdapter(titles);
drawerItemClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedPos = mRecyclerView.getChildPosition(v);
drawer.closeDrawers();
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_content, fragment[selectedPos])
.commit();
}
};
mRecyclerView.setAdapter(mAdapter);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
drawer,
toolbar,
R.string.open_drawer,
R.string.close_drawer) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
supportInvalidateOptionsMenu();
}
};
drawer.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_content, fragment[0])
.commit();
}
}
}
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize" />
<FrameLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
TabsFragment.java
public class TabsFragment extends Fragment {
public static Fragment newInstance() {
TabsFragment fragment = new TabsFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tabs, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new SamplePagerAdapter(getActivity().getSupportFragmentManager(), getActivity().getApplicationContext()));
SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setDistributeEvenly(true);
mSlidingTabLayout.setViewPager(mViewPager);
}
class SamplePagerAdapter extends FragmentPagerAdapter {
private String[] tabTitles = new String[] {"Tab One", "Tab Two"};
private Context context;
public SamplePagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return tabTitles.length;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
android.support.v4.app.Fragment[] fragment = {TabOneFragment.newInstance(), TabTwoFragment.newInstance()};
return fragment[position];
}
}
}
fragment_tabs.xml
<RelativeLayout
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"
tools:context=".TabsFragment">
<android.com.drawertab.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/sliding_tabs" />
</RelativeLayout>
TabsOneFragment.java
public class TabOneFragment extends Fragment{
public static TabOneFragment newInstance() {
TabOneFragment fragment = new TabOneFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab_one, container, false);
}
}
fragment_tabs_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center"
android:id="#+id/text"
android:text="TAB ONE" />
</LinearLayout>
TabsTwoFragment.java
public class TabTwoFragment extends Fragment {
public static TabTwoFragment newInstance() {
TabTwoFragment fragment = new TabTwoFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab_two, container, false);
}
}
fragment_tabs_two.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center"
android:id="#+id/text"
android:text="TAB TWO" />
</LinearLayout>
TextFragment.java
public class TextFragment extends Fragment {
public static Fragment newInstance() {
TextFragment fragment = new TextFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_text, container, false);
}
}
fragment_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center"
android:id="#+id/text"
android:text="FRAGMENT TEXT" />
</LinearLayout>
DrawerAdapter.java
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.ViewHolder> {
private String mNavTitles[];
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ViewHolder(View itemView,int ViewType) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.rowText);
itemView.setOnClickListener(MainActivity.drawerItemClickListener);
}
}
public DrawerAdapter(String[] titles) {
mNavTitles = titles;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_item_row, parent, false);
ViewHolder vhItem = new ViewHolder(v, viewType);
return vhItem;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(mNavTitles[position]);
}
#Override
public int getItemCount() {
return mNavTitles.length;
}
}
drawer_item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingTop="4dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/rowText" />
</LinearLayout>
If you are correct about sliding more times than twice, then you need to set the override method getCount correctly (I realize you know). That's the only issue you need to worry about.
private String[] tabTitles = new String[] {"Tab One", "Tab Two"};
...
#Override
public int getCount() {
return tabTitles.length;
}
Note: I declared tabTitles differently as an array of String "String[]", widely accepted. The other style "tabTitles[]" may not work well with its methods like length.
Looking at your code more closely, I suspect objects TabOneFragment and TabTwoFragment is not working well. Normally override getItem returns the same static Fragment. And of course, I do not see that declaration or code for it.
My suggested try is (from your code):
#Override
public android.support.v4.app.Fragment getItem(int position) {
return TabsFragment.newInstance(position);
}
Note: With the above code, I don't see any use for TabOneFragment and TabTwoFragment, referenced in getItem(int position) in SamplePagerAdapter class. The TextView UI in layout fragment_tab_one.xml is only used for the tab texts. For those texts, the override getPageTitle(int position) can be used and is already coded.

Categories

Resources