As the title says, I have a problem with RecyclerView and CardView.
During the development process, dark frames appeared over RecyclerView that isn't defined anywhere. Any advice how can I get rid of it?
CardView has two textViews and MapView.
SingleRun is a simple object with 2 Strings.
MainActivity
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Main Activity onCreate");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new SingleRunsAdapter(new SingleRunProvider().readData(), getApplicationContext()));
CardViewRecyclerViewItem
CardViewRecyclerViewItem
public class CardViewRecyclerViewItem extends CardView {
protected MapView mapView;
protected TextView distance;
protected TextView time;
public CardViewRecyclerViewItem(Context context) {
this(context, null);
}
public CardViewRecyclerViewItem(Context context, AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(getContext()).inflate(R.layout.card_view_single_run, this);
mapView = (MapView) view.findViewById(R.id.single_run_map_mapview);
distance = (TextView) view.findViewById(R.id.details_distance_textview);
time = (TextView) view.findViewById(R.id.details_time_textview);
}
public void cardViewOnCreate(Bundle savedInstanceState) {
if (mapView != null) {
mapView.onCreate(savedInstanceState);
}
}
public void cardViewOnResume() {
if (mapView != null) {
mapView.onResume();
}
}}
CardViewHolder
public class CardViewHolder extends RecyclerView.ViewHolder {
private CardViewRecyclerViewItem mCardViewRecyclerViewItem;
public CardViewHolder(CardViewRecyclerViewItem cardViewRecyclerViewItem) {
super(cardViewRecyclerViewItem);
mCardViewRecyclerViewItem = cardViewRecyclerViewItem;
}
public void cardViewRecyclerViewItemOnResume() {
if (mCardViewRecyclerViewItem != null) {
mCardViewRecyclerViewItem.cardViewOnResume();
}
}}
SingleRunAdapter
public class SingleRunsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
Context context;
private final List<SingleRun> singleRuns;
public SingleRunsAdapter(List<SingleRun> singleRuns, Context context) {
this.singleRuns = singleRuns;
this.context = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardViewRecyclerViewItem cardViewRecyclerViewItem = new CardViewRecyclerViewItem(context);
cardViewRecyclerViewItem.cardViewOnCreate(null);
return new CardViewHolder(cardViewRecyclerViewItem);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
CardViewHolder cardViewHolder = (CardViewHolder) viewHolder;
cardViewHolder.cardViewRecyclerViewItemOnResume();
}
#Override
public int getItemCount() {
return singleRuns.size();
}}
activity_main.xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.coderspeak.lightweightrunningtracker.single_run.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<LinearLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.coderspeak.lightweightrunningtracker.single_run.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</LinearLayout>
card_view_single_run.xml
<android.support.v7.widget.CardView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_margin="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.gms.maps.MapView xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/single_run_map_mapview"
android:layout_width="match_parent"
android:layout_height="144dp"
map:liteMode="true"
map:mapType="normal"
tools:context=".MapsActivity" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_margin="8dp">
<TextView
android:id="#+id/single_run_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="00:12:34"
android:textAlignment="center" />
<TextView
android:id="#+id/single_run_distance"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="3.14km"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
How it looks:
RecyclerView with strange background color
If you will comment both LinearLayouts with content in card_view_single_run.xml and mapView line in CardViewRecyclerViewItem you will see this:
THIS
This is more strange in my opinion, because even if empty cards, recycler have some background.
Thank you for any help. If necessary, I can provide more code.
The best way to diagnose this kind of errors IMO is using Layout Inspector tool in Android Studio:
Try the following
in your MainActivity
change context from getApplicationContext() to MainActivity.this
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new SingleRunsAdapter(new SingleRunProvider().readData(), MainAcitivity.this));
Related
This question already has answers here:
CardViews not showing in RecyclerView
(3 answers)
Closed 2 years ago.
activity_driver_list.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DriverListActivity">
<include
android:id="#+id/toolbarDL"
layout="#layout/toolbar"/>
<RelativeLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</LinearLayout>
fragment_driver_list.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".DLFragment.DriverListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/driverList"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
raw_driver_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="5dp"
app:cardCornerRadius="4dp"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="95dp"
android:layout_height="95dp"
android:padding="10dp"
android:src="#drawable/ic_android"
app:civ_border_width="2dp"
app:civ_border_color="#color/quantum_lightgreen"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/carName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Suzuki Abcd"
android:text="SUZUKI Wagon r"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:textColor="#color/places_text_black_alpha_87"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/driverNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="0312-1234567"
android:text="0312-1234567"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:textColor="#color/cardview_dark_background"
android:textSize="15sp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
DriverListAdapter.Java
public class DriverListAdapter extends RecyclerView.Adapter<DriverListAdapter.DriverListViewHolder> {
private ArrayList<Driver> mLst;
public class DriverListViewHolder extends RecyclerView.ViewHolder{
public CircleImageView mImage;
public TextView mCName, mDNumber;
public DriverListViewHolder(#NonNull View itemView) {
super(itemView);
mImage = itemView.findViewById(R.id.profile_image);
mCName = itemView.findViewById(R.id.carName);
mDNumber = itemView.findViewById(R.id.driverNumber);
}
}
#NonNull
#Override
public DriverListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_driver_list, parent, false);
DriverListViewHolder driverListAdapter = new DriverListViewHolder(view);
return driverListAdapter;
}
#Override
public void onBindViewHolder(#NonNull DriverListViewHolder holder, int position) {
Driver driver = mLst.get(position);
holder.mImage.setImageResource(driver.getdImage());
holder.mCName.setText(driver.getdCName());
}
#Override
public int getItemCount()
{
return mLst.size();
}
public DriverListAdapter(ArrayList<Driver> lst) {
this.mLst = lst;
}
}
DriverListFragment.Java
public class DriverListFragment extends Fragment {
public static final String TAG = "DriverListFragment";
private ArrayList<Driver> driverLst;
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private DriverListAdapter mAdapter;
public DriverListFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_driver_list, container, false);
driverLst = new ArrayList<>();
driverLst.add(new Driver(R.drawable.ic_android,"Car One","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Two","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Three","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Four","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Five","0312-1234567"));
mRecyclerView = (RecyclerView) v.findViewById(R.id.driverList);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this.getActivity());
mAdapter = new DriverListAdapter(driverLst);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return v;
}
}
DriverListActivity.Java
public class DriverListActivity extends AppCompatActivity {
public static final String TAG = "DriverListActivity";
private TDD tdd = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_list);
initToolbar();
getMyIntent();
driverList();
}
private void getMyIntent()
{
Intent i = getIntent();
tdd = (TDD) i.getSerializableExtra("tdd");
Log.d(TAG,tdd.toString());
}
private void initToolbar()
{
Toolbar toolbar = findViewById(R.id.toolbarDL);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Hello There..");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void driverList()
{
DriverListFragment driverListFragment = new DriverListFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer,driverListFragment);
fragmentTransaction.addToBackStack("driverListFragment");
fragmentTransaction.commit();
}
}
I want to show my RecyclerView list in fragment instead of an activity, when i call fragment in activity it simply showing me blank activity however, there's no error in code. How can i achieve this task or where i am doing a mistake?
How to build a Horizontal ListView with RecyclerView?
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);
I am using RecyclerView to show some data in form of list, but the data won't display.
Following is my code, I am using to implement RecyclerView
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
String[] c_names={"INDIA","US","UK","AUSTRALIA","CHINA","JAPAN"};
int[] c_flags={R.drawable.india,R.drawable.india,R.drawable.india,R.drawable.india,R.drawable.india,R.drawable.india};
private RecyclerView recyclerView;
RecyclerAdapter recyclerAdapter;
ArrayList<Country> arrayList=new ArrayList<>();
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView=findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setHasFixedSize(true);
int count=0;
for(String Name:c_names){
arrayList.add(new Country(Name,c_flags[count]));
Log.i("ggg", "onCreate: "+arrayList.get(count).getName()+" "+arrayList.get(count).getFlag_id());
count++;
}
recyclerAdapter=new RecyclerAdapter(arrayList);
recyclerView.setAdapter(recyclerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
MenuItem menuItem=menu.findItem(R.id.ction_search);
SearchView searchView= (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
newText=newText.toLowerCase();
ArrayList<Country> newList=new ArrayList<>();
for(Country country:arrayList){
String name=country.getName().toLowerCase();
if(name.contains(newText)){
newList.add(country);
}
}
recyclerAdapter.setFilter(newList);
return true;
}
}
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
ArrayList<Country> arrayList=new ArrayList<>();
public RecyclerAdapter(ArrayList<Country> arrayList) {
this.arrayList = arrayList;
}
#Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,
parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, int position) {
holder.c_flags.setImageResource(arrayList.get(position).getFlag_id());
holder.c_name.setText(arrayList.get(position).getName());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
ImageView c_flags;
TextView c_name;
public MyViewHolder(View itemView) {
super(itemView);
c_flags=itemView.findViewById(R.id.flag);
c_name=itemView.findViewById(R.id.name);
}
}
public void setFilter(ArrayList<Country> filter){
// filter=new ArrayList<>();
arrayList.addAll(filter);
notifyDataSetChanged();
}
}
activity_main
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="pritish.sawant.com.filterrecyclerview.MainActivity">
<include layout="#layout/toolbar_layout"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerview"/>
</LinearLayout>
row_layout
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="4dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="4dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="50dp"
android:elevation="5dp"
app:cardCornerRadius="8dp"
android:background="#9E9E9E"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:background="#9E9E9E">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="8dp"
android:scaleType="fitXY"
android:id="#+id/flag"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/flag"
android:layout_marginLeft="20dp"
android:gravity="center"
android:layout_centerVertical="true"
android:textSize="15dp"
android:textStyle="bold"
android:id="#+id/name"
android:textColor="#000000"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
toolbar_layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?attr/colorPrimary"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
My RecyclerView is blank.
I am showing the arrays mentioned in MainActivity in my RecyclerView. I tried a lot but couldn't figure out what am I doing wrong? Any help would be greatly appreciated.
You are missing orientation in parent layout.
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
tools:context="pritish.sawant.com.filterrecyclerview.MainActivity">
<include layout="#layout/toolbar_layout"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerview"/>
</LinearLayout>
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();
}
});
}
I have a RecyclerView with GridLayoutManager. On click of each Item i want tstart different acticities. I have tried but it is not working. This is my DashboardAdapter class
public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.ViewHolder>{
private List<DashboardPojo> countries;
private int rowLayout;
private Context mContext;
public DashboardAdapter(List<DashboardPojo> countries, int rowLayout, Context context) {
this.countries = countries;
this.rowLayout = rowLayout;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
DashboardPojo country = countries.get(i);
viewHolder.countryName.setText(country.name);
viewHolder.countryImage.setImageResource(country.getImageResourceId(mContext));
}
#Override
public int getItemCount() {
return countries == null ? 0 : countries.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView countryName;
public ImageView countryImage;
public ViewHolder(View itemView) {
super(itemView);
mContext = itemView.getContext();
itemView.setOnClickListener(this);
countryName = (TextView) itemView.findViewById(R.id.countryName);
countryImage = (ImageView)itemView.findViewById(R.id.countryImage);
}
#Override
public void onClick(View v) {
final Intent intent;
if (getPosition() == 0){
intent = new Intent(mContext, TutorialActivity.class);
} else if (getPosition() == 1){
intent = new Intent(mContext, JobsActivity.class);
} else {
intent = new Intent(mContext, TutorialActivity.class);
}
mContext.startActivity(intent);
}
}
}
And in my DashBoardActivity i have
mRecyclerView = (RecyclerView) findViewById(R.id.list);
mLayoutManager = new GridLayoutManager(this, 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mAdapter = new DashboardAdapter(DashboardManager.getInstance().getCountries(), R.layout.dashboard_row, this);
mRecyclerView.setAdapter(mAdapter);
And is dashboard_row.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
card_view:cardCornerRadius="5dp"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/countryImage"
android:layout_width="match_parent"
android:layout_height="120dp"
android:scaleType="centerCrop"
android:tint="#color/photo_tint"
android:layout_centerInParent="true"
/>
<TextView
android:id="#+id/countryName"
android:gravity="center"
android:background="?android:selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="24sp"
android:layout_centerInParent="true"
android:textColor="#android:color/white"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
activity_dashboard.xml
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rel"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashBoardActivity"
android:layout_below="#+id/app_bar"
android:layout_marginTop="20dp"/>
</RelativeLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="solutions.techieweb.com.techiewebsolutions.NavigationDrawerFragment"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
Please help me....
From what u have posted it seems that your View onClick() event is consumed by TextView
Please remove the :
android:focusable="true"
android:clickable="true"
tag from countryname TextView
And also change getPosition() to getAdapterPosition() as getPosition() is deprecated
Try This:
v.getContext().startActivity(intent);
Instead of:
mContext.startActivity(intent);
When I make Nested RecyclerView in android application, It is throws NPE.
activity_main.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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="#+id/groupList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
group_row.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="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF0000"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/shortcutList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
</LinearLayout>
Here is the exception
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
at android.view.View.measure(View.java:16538)
I need help. Why It is NullPointerException? Can I Fix this problem?
UPDATE
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView groupList = (RecyclerView) findViewById(R.id.groupList);
groupList.setAdapter(new GroupAdapter());
}
GroupAdapter.java
public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.ViewHolder> {
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.group_row, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
}
#Override
public int getItemCount() {
return 0;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
}
Add this:
LinearLayoutManager llm1 = new LinearLayoutManager(this);
groupList.setLayoutManager(llm1);