I have a RecyclerView with GridLayoutManager and when using SwipeRefreshLayout the RecyclerView is not pulled down. How to create the effect of having a gap between the toolbar and the recyclerview while refreshing?
XML:
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
CODE:
swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
gridView = (RecyclerView) getActivity().findViewById(R.id.gridView);
gridLayoutManager = new GridLayoutManager(getActivity(), 4, GridLayoutManager.VERTICAL, false);
gridView.setLayoutManager(gridLayoutManager);
gridView.setVisibility(View.GONE);
gridView.setHasFixedSize(false);
gridView.setItemAnimator(new DefaultItemAnimator());
Your code is all okay, but the swipeRefreshLayout does not pull down the grid view but only itself, For the demo check this video. You can however use other libraries like Ulta-Pull-to-Refresh to achieve the desired effect!
Related
I have a horizontalScrollView in my android app that I am trying to dynamically put a cardview with an imageView and a textView inside of the Horizontal scrollView when a button is pressed. When my app is first started up you see just a button named on the bottom of the app "findCar". When the button is pressed a method called buildCarView is called tha should put a cardView, textview and another button inside the upper portion of the screen (above the findCar button). But for some reason when i press the button nothing appears even though when I debug it the method to inflate the View is being called and the cardview/textView and the other button is being populated.
Here are the two XML's
This is the first thing you see when you open the app. It called dailouge_container.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<Button
android:id="#+id/find_car_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginBottom="292dp"
android:text="Find Car"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/scrollView"
app:layout_constraintVertical_bias="0.0" />
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="310dp"
android:layout_height="300dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/container"
android:orientation="horizontal"></LinearLayout>
</HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Below is the XML am trying to dynamically add when the button findCar its called Card.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:layout_width="280dp"
android:layout_height="230dp"
android:id="#+id/cardView"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageViewScroll"
android:layout_width="270dp"
android:layout_height="90dp"
/>
<TextView
android:id="#+id/price"
android:layout_width="270dp"
android:layout_height="50dp"
android:text="Price"
android:textSize="25dp">
</TextView>
<TextView
android:id="#+id/makeandmodel"
android:layout_width="270dp"
android:layout_height="50dp"
android:text="Make and Model"
android:textSize="22dp">
</TextView>
<Button
android:id="#+id/rentCarButton"
android:layout_width="270dp"
android:layout_height="35dp"
android:layout_marginBottom="4dp"
android:text="Rent Car"
android:textColor="#color/cardview_dark_background"
android:textSize="14dp"
></Button>
</LinearLayout>
</androidx.cardview.widget.CardView>
Below is what im doing in the OnCreate
LinearLayout linearLayout;
HorizontalScrollView horizontalScrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialoge_container);
mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
userId = currentUser.getUid();
DatabaseReference lendersToRentFrom = FirebaseDatabase.getInstance().getReference().child("Users").child("Lenders");
GeoFire geoFire = new GeoFire(lendersToRentFrom);
horizontalScrollView = findViewById(R.id.scrollView);
linearLayout = findViewById(R.id.container);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
Below is the findCar method that should dynamically add the cardView/textveiw and other button the horizontal scrollView
public void buildCardView(){
View view = getLayoutInflater().inflate(R.layout.card,null);
CardView cardView = view.findViewById(R.id.cardView);
Button rentCarButton = view.findViewById(R.id.rentCarButton);
TextView price= view.findViewById(R.id.price);
TextView makeandmodel = view.findViewById(R.id.makeandmodel);
ImageView imageView =view.findViewById(R.id.imageViewScroll);
linearLayout.addView(view);
}
I have a navigation drawer activity that contains a fragment that contains a RecyclerView. This RecyclerView works perfectly if it is the only component in this fragment, but I want to add a button above the RecyclerView and therefore insert the RecyclerView and the button in a LinearLayout. But then the RecyclerView has disappeared or no longer shows any elements.
How can I embed this RecyclerView in a layout?
RecyclerView works with this one:
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
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/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:name="com.company.myapp.ui.ItemFragment"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.home.HomeFragment"
tools:listitem="#layout/fragment_item" />
RecyclerView has disappeared in this one:
<?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"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Item" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:name="com.company.myapp.ui.ItemFragment"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.home.HomeFragment"
tools:listitem="#layout/fragment_item" />
</LinearLayout>
See complete project here: https://github.com/jriegraf/MyApp
You have this code in HomeFragment:
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS, mListener));
}
This is your problem. Once you wrap RecyclerView into LinearLayout, this
if (view instanceof RecyclerView)
isn't true any more, because now, view's type is LinearLayout.
If the view doesn't remove the recyclerView, you can ommit the if statement, and change this line:
RecyclerView recyclerView = (RecyclerView) view;
into:
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
My app has main, chat style, activity made of a RecycleView which is filled by a list. the recycle should scroll/focus to the last item of the list after the user insert a new item (text input, photo/video capture) but recycler keeps focus on the first item inside the list even after using recyclerView.scrollToPosition(CustomList.size()-1).
complete code available at: https://github.com/MikeSys/ChatApp
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
// Elementi Grafici-----------------------------------------
RecyclerView recyclerView = findViewById(R.id.recyclerView);
Button video = findViewById(R.id.video);
Button camera = findViewById(R.id.camera);
Button send = findViewById(R.id.send);
videoView =findViewById(R.id.rec);
final EditText editText = findViewById(R.id.editText);
// Media Controller----------------------------------------
// Layout Manager------------------------------------------------
linearLayoutManager = new LinearLayoutManager(MainActivity.this);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
// Adapter-----------------------------------------
if(dati.size()> 1){
adapter = new myAdapter(dati, this);
adapter.notifyDataSetChanged();
adapter.notifyItemInserted(dati.size()-1);
recyclerView.setAdapter(adapter);
//Setted recyclerView to scroll to the last item of the List
recyclerView.scrollToPosition(dati.size()-1);
}
else{
adapter = new myAdapter(dati,this);
recyclerView.setAdapter(adapter);
}
MainActivity Layout
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<android.support.v7.widget.RecyclerView
android:background="#drawable/sfondo"
android:foregroundGravity="center"
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/editText"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp"
app:stackFromEnd="true"/>
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginEnd="160dp"
android:background="#0003A9F4"
android:hint="Scrivi"
android:padding="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="3"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/send"
android:layout_width="55dp"
android:layout_height="64dp"
android:background="#drawable/send"
app:layout_constraintBottom_toBottomOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/editText"
app:layout_constraintTop_toTopOf="#+id/editText"
app:layout_constraintVertical_bias="0.0"/>
<Button
android:id="#+id/camera"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
android:background="#drawable/camera"
app:layout_constraintBottom_toBottomOf="#+id/editText"
app:layout_constraintEnd_toStartOf="#+id/send"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/editText"
app:layout_constraintTop_toTopOf="#+id/editText"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/video"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/video"
app:layout_constraintEnd_toStartOf="#+id/camera"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/editText"
app:layout_constraintTop_toBottomOf="#+id/recyclerView" />
Screenshots
use
recyclerView.smoothScrollToPosition(position);
position is your index of last item inserted. This will move your the RecyclerView focus on the last element.
I am adding dynamic imagebuttons to a layout in a Fragment using recyclerview with gridlayoutmanager. The buttons are added as a user performs an action, so they are not all created during onCreateView().
I seem to have to initialize my recycler view and adapter onCreateView() with a an empty imagebutton, when my app starts up. And so I do that, but it creates a little grey square like the image below.
and then when the user performs an action and the real button I want is created, the square is still present over my image button that was just created like you can see in this new image below.
DOES ANYONE KNOW HOW I CAN GET THE INITIAL EMPTY GREY IMAGE BUTTON TO NOT BE THERE AT STARTUP OR OVER MY "DriveDroid" IMAGE BUTTON?
I have tried setting the initial background of my image button to transparent (using background color = #00000000), but this seems to make a transparent image button over my DriveDroid button, so that my onClick listener for DriveDroid no longer works.
Here is how I initialize my recycler view:
public class MyFragment extends Fragment {
private GridLayoutManager lLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
// Create an empty list to initialize the adapter (or else get nullPointerException error)
List<ItemObject> myList = new ArrayList<ItemObject>();
lLayout = new GridLayoutManager(getActivity(), 4, GridLayoutManager.HORIZONTAL, false);
RecyclerView rView = (RecyclerView)view.findViewById(R.id.recycler_view);
rView.setHasFixedSize(true);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(getActivity(),myList);
rView.setAdapter(rcAdapter);
return view;
}
And here is my layout my_fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_margin="5dp"
android:id="#+id/my_fragment"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_name"
android:layout_below="#+id/new_app_button"
android:layout_alignStart="#+id/new_app_button"
android:layout_alignLeft="#+id/new_app_button"
android:gravity="center"
/>
</RelativeLayout>
HERE IS HOW I CHANGED MY LAYOUT TO GET IT TO WORK, IF IT HELPS ANYONE ELSE!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_marginTop="15dp"
android:id="#+id/my_fragment"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_name"
android:gravity="center"
android:layout_below="#+id/new_app_button"
/>
</RelativeLayout>
</LinearLayout>
As mentioned in the comment, that your controls was overlapping each others, you should wrap them inside linear layout or add properties to make them positioned relative to each others.
Regarding the grey button this should be related to "new_app_button"
I am trying to make a simple Checkbook app, whose MainActivity stores a list of transactions. I would like a TextView at the top and bottom of the screen that show the account balance and an option to add a new transaction, respectively. I would like a list of transactions in between that scroll. I was able to implement a ListView and add a header and footer view, but if the transaction list exceeds the size of the screen the headers and footers can scroll off screen.
Is there any way to position a ListView within the linear layout, or freeze the headers/footers to stay on the screen?
Here is my XML file so far:
<TextView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/add_transaction_string">
</TextView>
And here is my onCreate, which has no syntax errors but I am unable to click the footerview to add a transaction:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbook);
ListView list = (ListView) findViewById(R.id.transaction_list_view);
// Create a new Adapter
mAdapter = new TransactionAdapter(list.getContext());
// Inflate footerView and headerView
LayoutInflater inflater = getLayoutInflater();
TextView headerView = (TextView) inflater.inflate(R.layout.header_view, null);
TextView footerView = (TextView) inflater.inflate(R.layout.footer_view, null);
// Set listener for footerView
footerView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent transactionIntent = new Intent(CheckbookActivity.this, AddTransactionActivity.class);
startActivityForResult(transactionIntent, ADD_TRANSACTION_REQUEST);
}
});
list.setAdapter(mAdapter);
}
use the below code. This will satisfy your requirement. I tried this and working for me.
Relative layout with below,above attributes. Relativelayout is better than Linear layout with weight method.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:text="ListView Heading" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="ListView Footer" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"
android:layout_above="#id/textView2"
></ListView>
The UI will like this
Try this way, hope this will help you to solve your problem.
Instead of using header/footer just put as below code in your XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/header_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_transaction_string">
</TextView>
</LinearLayout>
Yes, you can do it with weightsum and layout_weight in linearlayout and also you can create this type of view using RelativeLayout.
1) In LinearLayout just add weightsum="1" to your linearlayout and add layout_weight="0.2" to each of your header and footer and add layout_weight="0.6" to your listview.
2) In relativeLayout add alignParentTop to your header and alignParentBottom to your footer and set listview to layout_below="#+id/header" and layout_above="2+id/footer"
I found a possible solution for your problem from a similiar post. Hope this helps you.
For what you are trying to accomplish to freeze the header/footer. It will be easier to use a relative layout to position the header/footer then have your listview in the middle
<RelativeLayout ...>
<TextView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header_view"
android:layout_above="#+id/footer_view">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="#string/add_transaction_string">
</TextView>
</RelativeLayout>
You can use a LinearLayout for this task. But I don't recommend it as it's a bit "hacky".
Get all the elements in a array: Example:- (weatherArray)
Loop through all the elements :-
Example:-
mainLayout = ((LinearLayout)refreshObj.get("mainLayout"));
mainLayout.removeAllViews();
for (int i = 0; i < weatherArray.length(); i++) {
View childView = getLayoutInflater().inflate(R.layout.weather_row4_item, mainLayout,false);
TextView todayTempStatus = (TextView) childView.findViewById(R.id.todayTempStatus);
todayTempStatus.setText("");
}
This is an example without using listview, which we will populate lienarlayout using child view.