So in my case I am using a RecyclerView to display a list of items but when I try to hide the action bar while scrolling I get this annoying flickering of the screen. I have searched the web for an answer to my problem but so far there has been no luck. So can anyone tell me what I am doing wrong?
here is my java for hiding the action bar:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.container = container;
this.inflater = inflater;
timePassed = System.currentTimeMillis();
if(hasMoreResults) {
rootView = inflater.inflate(R.layout.home_zone, container, false);
rootView.findViewById(R.id.imgAdsMap).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(context, AdsMap.class));
}
});
recyclerViewZone = (RecyclerView) rootView.findViewById(R.id.recyclerViewZone);
recyclerViewZone.setVerticalScrollBarEnabled(true);
// 2. set layoutManger
manager = new LinearLayoutManager(context);
recyclerViewZone.setLayoutManager(manager);
recyclerViewZone.setItemAnimator(new DefaultItemAnimator());
recyclerViewZone.setOnScrollListener(new RecyclerView.OnScrollListener()
{
int mLastFirstVisibleItem = 0;
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE && !generatedAdsFromScroll)
{
generatedAdsFromScroll = true;
runSplitRequest(10);
}
}
//Hiding the Action Bar for better view of the listed items
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final int currentFirstVisibleItem = manager.findFirstVisibleItemPosition();
if (currentFirstVisibleItem > this.mLastFirstVisibleItem)
{
ActionBar bar = getActivity().getActionBar();
bar.hide();
myButton = (RadioGroup)getActivity().findViewById(R.id.radioButtonGroup);
myButton.setVisibility(View.GONE);
}
else if (currentFirstVisibleItem < this.mLastFirstVisibleItem)
{
ActionBar bar = getActivity().getActionBar();
bar.show();
myButton = (RadioGroup)getActivity().findViewById(R.id.radioButtonGroup);
myButton.setVisibility(View.VISIBLE);
}
this.mLastFirstVisibleItem = currentFirstVisibleItem;
}
});
runSplitRequest(20);
}
else
rootView = inflater.inflate(R.layout.error_internet_connection, container, false);
return rootView;
}
This is the XML of the recyclerview:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recyclerViewZone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"
tools:context=".MainActivity"
android:scrollbars="vertical" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgAdsMap"
android:src="#drawable/map_button_map_down"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
and the style of the action bar:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:windowActionBarOverlay">true</item>
</style>
<dimen name="image_detail_height">200dp</dimen>
So as you can see I have enabled the action bar to overlay. I have also tried with calling the overlay of the action bar in the activity I call the recyclerview class but the problem remains... no luck the flickering is still there and even if I add padding to the recyclervie XML i just get a black rectangle with the size of the action bar.
Here is the xml of the activity I display the recyclerview:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:background="#android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#353535"/>
<RadioGroup
android:id="#+id/radioButtonGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
z
<RadioButton
android:button="#null"
android:checked="true"
android:contentDescription="ZONA"
android:background="#drawable/tab_back_selector"
android:textColor="#color/tab_text_color"
android:textSize="12sp"
android:layout_weight="1"
android:id="#+id/tabZoneHome"
android:padding="14dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/home_tab_zone"
android:gravity="center"/>
<RadioButton
android:button="#null"
android:contentDescription="VISITADAS"
android:background="#drawable/tab_back_selector"
android:textColor="#color/tab_text_color"
android:textSize="12sp"
android:layout_weight="1"
android:id="#+id/tabVisitedHome"
android:padding="14dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/home_tab_visited"
android:gravity="center"
android:checked="false" />
<RadioButton
android:button="#null"
android:contentDescription="SUGERENCIAS"
android:background="#drawable/tab_back_selector"
android:textColor="#color/tab_text_color"
android:textSize="12sp"
android:layout_weight="1"
android:id="#+id/tabSuggestionsHome"
android:padding="14dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/home_tab_suggestions"
android:gravity="center"
android:checked="false" />
</RadioGroup>
<RelativeLayout
android:id="#+id/ad_detailholder"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="#drawable/quickview_top_gradient"/>
</RelativeLayout>
<!-- Framelayout to display Fragments -->
</LinearLayout>
<!-- Listview to display slider menu -->
Related
I'm trying to make the BottomSheetDialogFragment appear at the top of BottomNavigationView and below the BottomNavigationView, ie with a lower elevation.
The BottomSheetDialogFragment will feature a YouTube-like video player with a RecyclerView and expanding and collapsing the bottomsheet.
I already researched several suggestions and nothing solved. Including Stackoverflow.
I tried the two variants of the Show () method, as per the code below - but none worked.
I can do Expand, Collapsed and other actions, do not worry about it. The problem is to make the BottomSheet behind the BottomNavigationView and above the RecyclerView of the Activity.
CODE ACTIVITY
public class myController extends CommonActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//inflate the layout of the activity
setContentView(R.layout.controller_layout);
//other activity codes
//progressBar = findViewById(R.id.progressBar);
//contains initialization codes for views and recyclerview
//initViews();
}
OPTION 01: It's the CALL that everyone is pointing to BottomSheetDilogFragment, except BottomSheet is over BottomNavigationView
INITIALIZED when clicking on a item (thumbnail) of a RecyclerView in the layout of the activity/fragment
public void initBottomSheet(Bundle b){
VideoViewBottomSheet fragment = new VideoViewBottomSheet();
fragment.setArguments( b );
fragment.show( getSupportFragmentManager(), VideoViewBottomSheet.FRAGMENT_KEY );}
OPTION 02: I tried this option for BottomSheetDilogFragment, in it the BottomSheet is
DOWN of the BottomNavigationView, BUT behave like a NORMAL FRAGMENT and not as BottomSheetDilogFragment, DO NOT EXPAND, DO NOT COLLAPSE, NOTHING.
public void initBottomSheet(Bundle b){
VideoViewBottomSheet fragment = new VideoViewBottomSheet();
fragment.setArguments( b );
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.bottom_sheet_loyout, fragment);
ft.commit();
}
}
CODE BottomSheetFragment
#Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
View view = getView();
final View parent = (View) view.getParent();
final View bottomSheet = dialog.findViewById(R.id.rv_bottom_sheet);
final View videoView = bottomSheet.findViewById(R.id.videoView);
final View bottom_nav_view = getActivity().findViewById(R.id.bottom_nav_view);
final int screenHeight = ViewGroup.LayoutParams.MATCH_PARENT;
final View relativeLayout = getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.relativelayout);
final View coodinatorLayout = getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.coordinatorlayout);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
parent.setFitsSystemWindows(false);
bottomSheet.getLayoutParams().height = screenHeight;
params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
final BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
if (bottomSheetBehavior instanceof BottomSheetBehavior) {
offsetY = 0;
((BottomSheetBehavior) bottomSheetBehavior).setBottomSheetCallback(
new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
if(slideOffset>0.2 ){
bottomSheetBehavior.setHideable(false);
bottomSheetBehavior.setPeekHeight(bottom_nav_view.getHeight()+56 );
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
parent.setPadding(0,0,0,bottom_nav_view.getHeight()+56);
parent.setBackgroundColor(Color.GREEN);
bottom_nav_view.setVisibility(View.VISIBLE);
bottom_nav_view.setBackgroundColor(Color.YELLOW);
}
offsetY = slideOffset;
}
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
}
});
}
}
}
The expected result is a BottoSheet similar to that of the Youtube Player. That is, a BottomSheet that contains a Fragment RecyclerView and Play Videos.
XMLS
mycontroller_layout.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:id="#+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:fitsSystemWindows="false"
tools:context=".myController">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinatorlayout"
android:layout_above="#+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:fitsSystemWindows="false">
<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.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="gone" />
<include layout="#layout/content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bottomsheet"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_nav_view"
android:layout_below="#+id/coordinatorlayout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:elevation="50dp"
android:visibility="visible"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:itemIconTint="#color/colorWhite"
android:background="#color/colorPrimary"
app:menu="#menu/activity_main_bottom" />
</RelativeLayout>
content.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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="4dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".RebootController"
tools:showIn="#layout/reboot_controller_layout">
<RelativeLayout
android:id="#+id/containerContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</RelativeLayout>
</RelativeLayout>
bottom_sheet_loyout.xml
-------------------
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rv_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_nav_view"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:elevation="3dp"
android:fitsSystemWindows="false"
android:orientation="vertical"
app:layout_behavior="#string/bottom_sheet_behavior" >
<RelativeLayout
android:id="#+id/rlVideoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/srl_swipe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/videoView">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:clipToPadding="false"
android:paddingBottom="56dp"/>
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:visibility="gone" />
<!--<LinearLayout
anúncio google
</LinearLayout>-->
</RelativeLayout>
</RelativeLayout>
I am trying to make layout like in the picture. Project picture I can only link it because I am new. When I run the code with just one LinearLayout it works fine. But when I make nested linear layout it shows an only white screen. And there are no errors. The code adds a score to teams according to their throw s.Thank you.
Java code:
public class MainActivity extends AppCompatActivity {
int scorea=0;
int scoreb=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void displayforpointb3(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb+=3;
scoreView.setText(""+scoreb);
}
public void displayforpointb2(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb+=3;
scoreView.setText(""+scoreb);
}
public void displayforfreethrowb(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb++;
scoreView.setText(""+scoreb);
}
public void displayforpoint3(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea+=3;
scoreView.setText(""+scorea);
}
public void displayforpoint2(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea+=2;
scoreView.setText(""+scorea);
}
public void displayforfreethrow(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea++;
scoreView.setText(""+scorea);
}
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.android.courtcounter.MainActivity"
android:orientation="horizontal">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Teama"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text="#string/zero"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:id="#+id/team_a_score" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/point3"
android:onClick="displayforpoint3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/point2"
android:onClick="displayforpoint2"/>
<Button
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:layout_height="wrap_content"
android:text="#string/freethrow"
android:onClick="displayforfreethrow"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Teama"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/zero"
android:id="#+id/team_b_score" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="#string/point3"
android:onClick="displayforpointb3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="#string/point2"
android:layout_weight="1"/>
android:onClick="displayforpointb2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/freethrow"
android:onClick="displayforfreethrowb"/>
</LinearLayout>
</LinearLayout>
First of all you should define your TextView as a private attributes in your class as follows
private TextView scoreViewA;
private TextView scoreViewB;
Then in your onCreate() method, you should find the view of those TextView as follows
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
scoreViewA = findViewById(R.id.team_a_score);
scoreViewB = findViewById(R.id.team_b_score);
}
Then in your onClick methods, you should update those TextView
public void displayforpointb3(View view){
scoreb+=3;
scoreViewB.setText(""+scoreb);
}
public void displayforpointb2(View view){
scoreb+=2; //You had scoreb+=3 I changed it to 2
scoreViewB.setText(""+scoreb);
}
public void displayforfreethrowb(View view){
scoreb++;
scoreViewB.setText(""+scoreb);
}
public void displayforpoint3(View view){
scorea+=3;
scoreViewA.setText(""+scorea);
}
public void displayforpoint2(View view){
scorea+=2;
scoreViewA.setText(""+scorea);
}
public void displayforfreethrow(View view){
scorea++;
scoreViewA.setText(""+scorea);
}
Also I see something wrong in your activity_main.xml I guess is a misstype
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="#string/point2"
android:layout_weight="1"/> <--- Remove this "/>"
android:onClick="displayforpointb2"/>
Happy coding
Problem
The orientation of the root LinearLayout is horizontal. This results in child views appearing to the right of the Toolbar instead of underneath it.
Solution
Make the root LinearLayout vertical and wrap the two inner view groups in a horizontal LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Team A -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Text & Buttons -->
</LinearLayout>
<!-- Team B -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Text & Buttons -->
</LinearLayout>
</LinearLayout>
</LinearLayout>
It's not pretty, but it gets the job done. Consider using ConstraintLayout as an alternative to nested view groups if you encounter any performance issues.
i am developing and app that should show users instagram posts in a recyclerview...i get all data correctly but after i scroll my recyclerview pictures get mess up and one picture shows twice ...and other messed ups in my list.
here is my onbind View holder in recyclerview extends RecyclerView.Adapter :
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
Picasso.with(context).
load(imageThumbList.get(position)
).
into(ivimage);
igetPositionImage.getPosition(position);
/*imageLoader.DisplayImage(imageThumbList.get(position), ivimage);*/
count_like.setText(likeCounts.get(position));
and its my called up recycle view in my fragment:
private void setImageGridAdapter() {
recyclerView = (RecyclerView) getView().findViewById(R.id.recycler_view);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(),3);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(1, dpToPx(1), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
RecycleListAdapter recycleListAdapter = new RecycleListAdapter(getActivity(),imageThumbList , likeCounts ,LikeGetterF.this);
recyclerView.setAdapter(recycleListAdapter);
Paginate.with(recyclerView , callbacks)
.setLoadingTriggerThreshold(2).
addLoadingListItem(true).
setLoadingListItemCreator(new RecycleListAdapter(getActivity(),imageThumbList,likeCounts,LikeGetterF.this))
.setLoadingListItemSpanSizeLookup(new RecycleListAdapter(getActivity(),imageThumbList,likeCounts,LikeGetterF.this))
.build();
//gvAllImages.setAdapter(new MyGridListAdapter(context,imageThumbList));
}
and my xml code :
> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/all_media_files"
>
<!-- <ListView -->
<!-- android:id="#+id/lvImages" -->
<!-- android:layout_width="fill_parent" -->
<!-- android:layout_height="wrap_content" > -->
<!-- </ListView> -->
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:background="#AFAFAFAF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:scrollbars="horizontal" />
</LinearLayout>
and my item adapter xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="20dp"
>
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp"
>
<ImageView
android:id="#+id/ivImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:clickable="true"
android:scaleType="fitCenter"
android:src="#drawable/profile"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#AFAFAFAF"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="#drawable/like_red"
android:id="#+id/like_img"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="0"
android:gravity="right"
android:textColor="#color/textColorPrimary"
android:textSize="#dimen/buttonSizes"
android:id="#+id/like_count"
/>
</LinearLayout>
</FrameLayout>
<!-- android:background="#drawable/char_02"-->
</LinearLayout>
Remove final for position in bind holder,
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int adapterPos=holder.getAdapterPosition();
Picasso.with(context).
load(imageThumbList.get(adapterPos)
).
into(ivimage);
igetPositionImage.getPosition(position);
holder.setIsRecyclable(false);
/*imageLoader.DisplayImage(imageThumbList.get(position), ivimage);*/
count_like.setText(likeCounts.get(position));
and use adapterPos everywhere in bindView holder
How to do add custom layout in NavigationView and design my create custom NavigationView use material design,i want put my drawer icon to right and text left of it something like this
I Search too much and this is my experience that works fine
at first create layout for header. its name is nav_header_main.xml then put it in layouts folders in res and put this code in it..
<?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="#dimen/nav_header_height"
android:background="#drawable/header"
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:theme="#style/ThemeOverlay.AppCompat.Dark"
android:gravity="top">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/cv_nave_profile_image"
android:layout_width="#dimen/nav_profile_image"
android:layout_height="#dimen/nav_profile_image"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/profile"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/cv_nave_profile_image"
android:layout_alignParentTop="true"
android:padding="#dimen/activity_horizontal_margin"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_nav_name"
android:textStyle="bold"
android:typeface="sans"
android:textColor="#ffffff"
android:gravity="right"
android:layout_gravity="right"
android:text="رخداد جدید"
android:paddingBottom="5dp"
android:textSize="#dimen/body"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:typeface="sans"
android:textColor="#ffffff"
android:id="#+id/tv_nav_phone"
android:layout_alignParentLeft="true"
android:text="0370077315"
/>
</RelativeLayout>
</LinearLayout>
then i include it as child of NavigationView and For menu item i use RecyclerView to show menu and icon so my NavigationView is
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="spydroid.ir.dorobar.Activities.SearchActivity">
<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.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_search" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="fill_parent" android:layout_height="match_parent"
android:layout_gravity="right" android:fitsSystemWindows="true"
android:layout_marginLeft="#dimen/nav_margin"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<include layout="#layout/nav_header_main" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/drawer_slidermenu"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"/>
</RelativeLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
just you have to remember put your NavigationView in DrawerLayout
then i create layout for menu item with ImageView and TextView this layout and this name is card_drawer_item.xml and its code is here
<?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="48dp">
<ImageView
android:id="#+id/drawer_icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="#drawable/ic_about"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/drawer_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/drawer_icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:typeface="sans"
android:paddingRight="40dp"/>
</RelativeLayout>
then i create ViewHolder folder for this layout.
public class DrawerItemHolder extends RecyclerView.ViewHolder {
public ImageView itemIcon;
public TextView itemText;
public DrawerItemHolder(View itemView) {
super(itemView);
itemIcon= (ImageView) itemView.findViewById(R.id.drawer_icon);
itemText= (TextView) itemView.findViewById(R.id.drawer_text);
}
}
now i define text of menu items as string array and array that contains menu icons in menu in strings.xml
<string-array name="drawer_items">
<item>setting</item>
<item>add record</item>
<item>ads</item>
<item>about</item>
<item>call</item>
<item>help</item>
<item>privacy</item>
</string-array>
<array name="drawers_icons">
<item>#drawable/ic_action_settings</item>
<item>#drawable/ic_plus</item>
<item>#drawable/ic_ads</item>
<item>#drawable/ic_about</item>
<item>#drawable/ic_phone</item>
<item>#drawable/ic_help</item>
<item>#drawable/ic_policy</item>
</array>
then we just need an Adapter like this
public class DrawerItemAdapter extends RecyclerView.Adapter<DrawerItemHolder> {
// slide menu items
private List<DrawerItem> items;
private List<Integer> drawerIcons;
public DrawerItemAdapter(List<DrawerItem> items) {
super();
this.items = items;
}
#Override
public DrawerItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.card_drawer_item, parent, false);
return new DrawerItemHolder(itemView);
}
#Override
public void onBindViewHolder(DrawerItemHolder holder, int position) {
holder.itemIcon.setImageResource(items.get(position).getIconId());
holder.itemText.setText(items.get(position).getText());
}
#Override
public int getItemCount() {
return items.size();
}
}
every thing is ok .. just now we have to set NavigationView in Activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
recList = (RecyclerView) findViewById(R.id.drawer_slidermenu);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
String []itemsTitle=getResources().getStringArray(R.array.drawer_items);
TypedArray icons=getResources().obtainTypedArray(R.array.drawers_icons);
List<DrawerItem>drawerItems= new ArrayList<DrawerItem>();
for(int i=0;i<itemsTitle.length;i++){
drawerItems.add(new DrawerItem(icons.getResourceId(i,-1),itemsTitle[i]));
}
DrawerItemAdapter ad= new DrawerItemAdapter(drawerItems);
recList.setAdapter(ad);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
return;
}
super.onBackPressed();
}
Toolbar is not appearing on pre lollipop devices. I'm using drawer layout with toolbar. The same toolbar works on other activities but not when used along with drawer layout.
Activity with drawer layout and toolbar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
{
/*supportChatImageButton = (ImageButton) findViewById(R.id.homeSupportChatImageButton);
orderImageButton = (ImageButton) findViewById(R.id.homeOrderImageButton);*/
headerTextView = (TextView) findViewById(R.id.homeHeaderTextView);
slidingTabLayout = (SlidingTabLayout) findViewById(R.id.homeSlidingTabLayout);
viewPager = (ViewPager) findViewById(R.id.homeViewPager);
headerBackgroundImageView = (ImageView)findViewById(R.id.homeHeaderBackgroundImageView);
mainContentLayout = (FrameLayout) findViewById(R.id.homeScreenMainContentLayout);
hamburgerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
hamburgerLinearLayout = (LinearLayout) findViewById(R.id.hamburgerProfileLayout);
hamburgerListView = (ListView) findViewById(R.id.hamburgerListView);
hamburgerMenu = (RelativeLayout) findViewById(R.id.hamburgerMenu);
GetHamburgerListItems();
HamburgerListAdapter hamburgerListAdapter = new HamburgerListAdapter(this, hamburgerListItems);
hamburgerListView.setAdapter(hamburgerListAdapter);
userProfileImageView = (ImageView) findViewById(R.id.userProfileImageView);
userProfileNameTextView = (TextView) findViewById(R.id.userProfileNameTextView);
if(StorageManager.read(this, "userProfile", new TypeToken<UserProfile>() {}.getType()) != null) {
UserProfile userProfile = (UserProfile) StorageManager.read(this, "userProfile", new TypeToken<UserProfile>() {}.getType());
userProfileNameTextView.setText(userProfile.getFullName());
}
else {
userProfileNameTextView.setText("Login / Sign up");
}
Typeface typeface = Typeface.createFromAsset(this.getAssets(), "fonts/GothamRnd-Medium.otf");
slidingTabLayout.setTypeface(typeface);
slidingTabLayout.setTextColor("#FFFFFF");
}
if(Utilities.IsCitySelected(this)) {
selectedCity = Utilities.GetSelectedCity(this);
}
else {
Intent intent = new Intent(HomeScreenActivity.this, ErrorInternetActivity.class);
intent.putExtra("error", "error");
startActivity(intent);
finish();
}
toolbar = (Toolbar) findViewById(R.id.main_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_action_image_dehaze);
toolbar.inflateMenu(R.menu.home_screen_menu);
toolbar.setBackgroundColor(Color.TRANSPARENT);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, hamburgerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
syncState();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
syncState();
}
};
hamburgerLayout.setDrawerListener(actionBarDrawerToggle);
hamburgerLayout.setDrawerListener(this);
hamburgerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
publ
ic void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
SelectItemFromDrawer(position);
}
});
hamburgerLinearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(Utilities.IsUserLoggedIn(HomeScreenActivity.this)) {
hamburgerLayout.closeDrawer(hamburgerMenu);
isLinkClicked = true;
position = 2;
}
else {
hamburgerLayout.closeDrawer(hamburgerMenu);
isLinkClicked = true;
position = 3;
}
}
}
});
Activity - XML
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeScreenActivity">
<FrameLayout
android:id="#+id/homeScreenMainContentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/main_toolbar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/homeHeaderBackgroundImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:gravity="right">
<!--<ImageButton
android:id="#+id/homeSupportChatImageButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginRight="20dp"
android:background="#drawable/ic_help"
android:visibility="visible" />
<ImageButton
android:id="#+id/homeOrderImageButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginRight="16dp"
android:background="#drawable/ic_menu_orders" />-->
</LinearLayout>
<com.misc.TextView
android:id="#+id/homeHeaderTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="#string/home_screen_header_text"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold"
app:fontName="#string/gotham_bold_reg" />
<com.misc.SlidingTabLayout
android:id="#+id/homeSlidingTabLayout"
fontPath="fonts/GothamRnd-Medium.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingTop="10dp"></com.misc.SlidingTabLayout>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:background="#FFFFFF">
<android.support.v4.view.ViewPager
android:id="#+id/homeViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"></android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
</FrameLayout>
<!-- The navigation drawer -->
<RelativeLayout
android:id="#+id/hamburgerMenu"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<!-- Profile Box -->
<LinearLayout
android:id="#+id/hamburgerProfileLayout"
android:layout_width="match_parent"
android:layout_height="144dp"
android:background="#color/lo_orange"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/userProfileImageView"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginLeft="16dp"
android:scaleType="fitXY"
android:src="#drawable/ic_person_blue" />
<com.misc.TextView
android:id="#+id/userProfileNameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
app:fontName="#string/gotham_medium_reg"
android:textSize="14sp"
android:text=""
android:textColor="#FFFFFF"/>
</LinearLayout>
<!-- List of Actions (pages) -->
<ListView
android:id="#+id/hamburgerListView"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_below="#id/hamburgerProfileLayout"
android:background="#FFFFFF"
android:choiceMode="singleChoice" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
main_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/lo_orange"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.misc.TextView
android:id="#+id/toolBarTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fontName="#string/gotham_medium_reg"
android:maxLines="2"
android:textSize="16sp"
android:textColor="#FFFFFF"/>
<com.misc.TextView
android:id="#+id/toolBarSubTitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fontName="#string/gotham_light_reg"
android:maxLines="1"
android:textSize="12sp"
android:visibility="gone"
android:textColor="#FFFFFF"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:textColorHintInverse">#color/lo_orange</item>
<!--<item name="colorPrimaryDark">#color/dark_orange</item>-->
<item name="colorPrimary">#color/lo_orange</item>
<item name="colorAccent">#color/lo_orange</item>
<!-- API 14 theme customizations can go here. -->
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
This is a common issue becoming less apparent when testing on Lollipop or later.
When using standard layouts (such as FrameLayout, RelativeLayout etc.) the default behavior is that child views get drawn in order they are added or inflated.
The fix would look similar to this:
<FrameLayout>
<!-- Content will be drawn first - below the toolbar. -->
<FrameLayout android:id="#+id/main_content" />
<!-- Toolbar will be drawn next - above the content. -->
<android.support.v7.widget.Toolbar android:id="#+id/toolbar" />
</FrameLayout>
However since Lollipop the android:elevation attribute can override this behavior. Since elevation defines precise position along the Z axis views with higher elevation values will be drawn above those with lower elevation values.