I am new to Android TV development. I don't know which type of fragment to use. I want two achieve layout similar to above screenshot as jiocinema. Somehow I have achieved it using a two xml fragments inside activity layout. Second fragments loads screenshot after hitting an API so it loads after some time. As can be seen in the above screenshot I want the layout in two parts..top one with details and some buttons and the bottom one is a list of screenshots of that movie.
In my case the problem is, bottom list part takes the focus on loading this particular screen after that on pressing up button or any button it never loses focus and never goes on the top part.
Note: below fragment loads asynchronously, as it hits an api for screenshot urls
May be I haven't used proper fragments for this particular layout. Can someone point me to the code or help me out in deciding what to use for this kind of layout. As it can be achieved but navigation is the main thing.
code
activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/photo_label_box">
<fragment
android:id="#+id/detail_layout"
android:layout_width="match_parent"
android:name="com.DetailsActivityGame$Detalfragment"
android:layout_height="200dp"></fragment>
<fragment
android:id="#+id/row_layout"
android:layout_width="match_parent"
android:layout_below="#+id/detail_layout"
android:name="com.DetailsActivityGame$SampleFragmentC"
android:layout_height="wrap_content"></fragment>
</RelativeLayout>
Thanks
Try to use the RowSupportFragment in V4 Support Fragment for desired output.
Divide layout into two parts layout with buttons, description and below scrolling layout(Represent by RowSupportFragment)
//----------------------detail_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/leader_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_layout"
//your own layout design for buttons and description
</RelativeLayout>
<fragment
android:name="FragmentScreenshots"
android:id="#+id/screenshot_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//----------------Detailfragment--------------------
public static class Detailfragment extends Fragment {
public Detailfragment(){ }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.detail_layout, container, false);
//————————your own implementation—————————————————————————
return view;
}
public static class FragmentScreenshots extends RowsSupportFragment {
private ArrayObjectAdapter mRowsAdapter = null;
public FragmentScreenshots() {
mRowsAdapter = new ArrayObjectAdapter(new ShadowRowPresenterSelector());
setAdapter(mRowsAdapter);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//———————Provide data accordinally———————————
List<ScreenshotItem> list;
// Add a Related items row
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(
new ScreenshotCardPresenter(getActivity()));
for (ScreenshotItem s:list)
{
listRowAdapter.add(s);
}
HeaderItem header = new HeaderItem("Screenshots");
mRowsAdapter.add(new ListRow(header, listRowAdapter));
setAdapter(mRowsAdapter);
setOnItemViewClickedListener(new OnItemViewClickedListener() {
#Override
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
if (item instanceof ScreenshotItem) {
}
else{
}
}
});
setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
#Override
public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
}
});
}
#Override
public void setExpand(boolean expand) {
super.setExpand(true);
}
#Override
public void setOnItemViewClickedListener(BaseOnItemViewClickedListener listener) {
super.setOnItemViewClickedListener(listener);
}
#Override
public void setOnItemViewSelectedListener(BaseOnItemViewSelectedListener listener) {
super.setOnItemViewSelectedListener(listener);
}
}}
You have to use BrowseFragment for your purpose. It is composed of a RowsFragment and a HeadersFragment.
A BrowseFragment renders the elements of its ObjectAdapter as a set of rows in a vertical list. The elements in this adapter must be subclasses of Row.
This tutorial can help you to get started.
Related
I am using fragments to update a text view I have so when the person clicks a button the text view moves on to the next question. I'm not sure if I am doing the correct work in one fragment instead of the other. My current screen looks like this:
I will probably have to add some more buttons/widgets to this but should I be adding it into the XML for the fragment or the fragment container?
Here is XML for fragment actions:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_question_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".FragmentActions"
>
<!-- this is where fragments will be shown-->
<FrameLayout
android:id="#+id/question_container1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:scaleType="centerInside" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/questions_yes1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/yes" />
<Button
android:id="#+id/questions_no1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/no" />
</LinearLayout>
</LinearLayout>
And here is the fragment details:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/button_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
tools:context=".FragmentDetails">
<!--Blank Fragment Layout-->
<TextView
android:id="#+id/questions_text_view1"
android:layout_width="match_parent"
android:layout_height="91dp"
android:gravity="center"
android:textAlignment="center"
/>
</FrameLayout>
Updated FragmentDetails
public class FragmentDetails extends Fragment {
private final String TAG = getClass().getSimpleName();
private List<Integer> mQuestionIds;
private int mListIndex;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the fragment layout
View rootView = inflater.inflate(R.layout.fragment_details, container, false);
//Get a reference to the textView in the fragment layout
final TextView textView = (TextView) rootView.findViewById(R.id.questions_text_view1);
if (mQuestionIds != null) {
textView.setText(mQuestionIds.get(mListIndex));
//Increment the position in the question lisy as long as index is less than list length
if (mListIndex < mQuestionIds.size() - 1) {
mListIndex++;
setmQuestionIds(QuestionList.getQuestions());
setmListIndex(mListIndex);
} else {
//end of questions reached
textView.setText("End of questions");
}
//Set the text resource to display the list item at that stored index
textView.setText(mQuestionIds.get(mListIndex));
}
else {
//Log message that list is null
Log.d(TAG, "No questions left");
}
//return root view
return rootView;
}
public void setmQuestionIds (List < Integer > mQuestionIds) {
this.mQuestionIds = mQuestionIds;
}
public void setmListIndex ( int mListIndex){
this.mListIndex = mListIndex;
}
}
Fragment Actions activity
public class FragmentActions extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_actions);
Button yes = findViewById(questions_yes1);
// Only create new fragments when there is no previously saved state
if (savedInstanceState == null) {
//Create Question Fragment
final FragmentDetails fragmentDetails = new FragmentDetails();
fragmentDetails.setmQuestionIds(QuestionList.getQuestions());
yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set the list of question Ids for the head fragent and set the position to the second question
//Fragment manager and transaction to add this fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.question_container1, fragmentDetails)
.commit();
}
});
}
}
}
If your Buttons remain the same while the TextView changes, you may add your Buttons to the fragment container.
Remember that, your fragments will be presented inside the FrameLayout of the fragment container. You gotta keep your Buttons, outside the FrameLayout.
Or if you want to have different Buttons for different fragments (Questions, in your case), you can also add the Buttons to the fragments. But in that case, you gotta add them separately to each of the fragments.
I guess there's no right answer to your question. You could try different approaches.
Maybe you could implement the buttons in the fragment container, as #smmehrab pointed out. I see this as a more difficult solution, because when you click on an item from the container you can manage the views of the container, not the fragment's views. You would get NullPointer if I recall correctly. This happens because the context when the button is clicked in the fragment container is different than the context when clicking from within the fragment. So you should implement an interface on the fragment container that listens to clicks, and the fragment catches the click. You could do this, and I actually am doing it in my current app, but I have no choice.
You could instead use Motion Layout (which extends from Constraint Layout) as the root view of your fragment, instead of CardView. This way you could set all the fragment's views with a flat hierarchy (flat hierarchies improves rendering time, so that's an improvement, and you can use CardView as one child) and set the buttons right there, in the Motion Layout (remember, the motion layout would be the fragment's root view). You could set the click listener right there and implement animations between different textViews.
I'm sure there are plenty of other solutions, take this only as a contribution.
If you're unfamiliar with Motion Layout you can just google it, android official documentation about it is great.
This is my fragment where I have called the recycler view:
public class sgpa_frag extends Fragment {
//View view;
RecyclerView recyclerview;
adapter_sgpa ac;
ArrayList<POJO> sgpaArrayList;
public sgpa_frag() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sgpa_frag, container, false);
recyclerview= (RecyclerView) view.findViewById(R.id.rc1);
sgpaArrayList= new ArrayList<>();
ac= new adapter_sgpa(sgpaArrayList);
recyclerview.setLayoutManager(new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,true));
recyclerview.setAdapter(ac);
Fetchdata1();
return view;
}
private void Fetchdata1()
{
dbmanager db= new dbmanager(getContext());
Cursor cursor= db.fetch_data1();
if (cursor!= null){
// cursor.moveToFirst();
while (cursor.moveToNext()){
POJO pj= new POJO();
pj.setSname(cursor.getString(0));
pj.setSemester(cursor.getString(1));
pj.setSgpa(cursor.getString(2));
pj.setPercent(cursor.getString(3));
pj.setSchemes(cursor.getString(4));
sgpaArrayList.add(pj);
}
ac= new adapter_sgpa(sgpaArrayList);
}
}
}
This is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".drawernav.bottom_navi.recycler_view.sgpa_frag">
<!-- TODO: Update blank fragment layout -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rc1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="60dp">
</androidx.recyclerview.widget.RecyclerView>
</FrameLayout>
I want my Recycler view to start displaying from the top item and not from the middle item . Can anyone tell me what changes should I make in my code ? Thanks in advance .
I mean to say that, it is like this:
And I want it to be like this:
You should set layout width and height of the recyclerview to match_parent.
The actual reason for your issue is probably the reverseLayout of your your linear layout manager. This has the effect as scrolling down the recyclerview.
Use this instead:
new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false)
Try adding
android:descendantFocusability="blocksDescendants"
to the recyclerview parent layout
Edit:
I think it is not a problem with RecyclerView instead if you are using a cordinator layout as root layout, try giving margin to your FrameLayout of 60
<!-- TODO: Update blank fragment layout -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rc1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop ="60dp" //approx for toolbar height
android:paddingBottom="60dp">
</androidx.recyclerview.widget.RecyclerView>
Add this into you code
mRecyclerView.smoothScrollToPosition(0);
Hi I've started learning android recently and am Trying to get familiar with RecyclerView.
I have a Main menu which is a simple list. When clicked on any item a new activity named subMenu starts. This SubMenu contains a RecyclerView where each item is a cardview having two buttons, that can be used to increase or decrease items quantity (which is also being shown card view). Now I want to retain changes made in Recyclerview for each main menu. Currently I have adapter for only one sub menu.
Any help or sample is appreciated.
Main menu xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainMenu">
<ListView
android:id="#+id/categories_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar">
</ListView>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Main menu .java file
public class MainMenu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
TextView textView=findViewById(R.id.editText);
Toolbar toolbar=findViewById(R.id.toolbar);
toolbar.setTitle("Table "+getIntent().getStringExtra("table_no"));
setSupportActionBar(toolbar);
final ListView listView=findViewById(R.id.categories_list);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.main_categories));
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(MainMenu.this,subMenu.class);
intent.putExtra("main_category",listView.getItemAtPosition(position).toString());
startActivity(intent);
}
});
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu,menu);
return true;
}
}
sub menu .xml file
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".subMenu">
<Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
</Toolbar>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview_submenu"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar2">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
Edit
To make my question more clear
Here is main menu
When clicked sub menu opens
The number on top right is quantity that can be incremented or decremented by user as shown below
Now when user moves to main menu and comes back to sub menu of same category these changes in quantity are not retained.
I hope I made myself clear. Simple example would be helpful.
Edit
Adapter code for sub menu RecyclerView.
public class RecyclerViewAdapterSubItems extends RecyclerView.Adapter<RecyclerViewAdapterSubItems.subItemViewHolder> {
private Context context;
private List<SubItems> subItemsList;
public RecyclerViewAdapterSubItems(Context context, List<SubItems> subItemsList){
this.context=context;
this.subItemsList=subItemsList;
}
#Override
public subItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater=LayoutInflater.from(context);
view=inflater.inflate(R.layout.cardview_sub_menu_item,parent,false);
return new subItemViewHolder(view);
}
#Override
public void onBindViewHolder(final subItemViewHolder holder, final int position) {
holder.subItemName.setText(subItemsList.get(position).getName());
holder.subItemPrice.setText(Integer.toString(subItemsList.get(position).getPrice()));
holder.subItemQuantity.setText(Integer.toString(subItemsList.get(position).getQuantity()));
holder.incrementQuantityButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
subItemsList.get(position).setQuantity(subItemsList.get(position).getQuantity()+1);
holder.subItemQuantity.setText(Integer.toString(subItemsList.get(position).getQuantity()));
}
});
}
#Override
public int getItemCount() {
return subItemsList.size();
}
public static class subItemViewHolder extends RecyclerView.ViewHolder {
TextView subItemName;
TextView subItemPrice;
TextView subItemQuantity;
ImageButton incrementQuantityButton;
ImageButton decrementQuantityButton;
public subItemViewHolder(View itemView) {
super(itemView);
subItemName=itemView.findViewById(R.id.textView_sub_item_name);
subItemPrice=itemView.findViewById(R.id.textView_sub__item_price);
subItemQuantity=itemView.findViewById(R.id.textView_sub_item_quantity);
incrementQuantityButton=itemView.findViewById(R.id.imageButton_increment_quantity);
decrementQuantityButton=itemView.findViewById(R.id.imageButton_decrement_quantity);
}
}
}
submenu.java
public class subMenu extends AppCompatActivity {
static List<SubItems> subItemList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_menu);
RecyclerView recyclerView=findViewById(R.id.recyclerview_submenu);
subItemList=new ArrayList<>();
subItemList.add(new SubItems("Pasta",20,0));
subItemList.add(new SubItems("Peparoney",20,0));
subItemList.add(new SubItems("asta",40,0));
RecyclerViewAdapterSubItems adapterSubItems=new RecyclerViewAdapterSubItems(this,subItemList);
recyclerView.setAdapter(adapterSubItems);
recyclerView.setLayoutManager(new GridLayoutManager(this,1));
}
}
Your issue is here:
RecyclerView recyclerView=findViewById(R.id.recyclerview_submenu);
subItemList=new ArrayList<>();
subItemList.add(new SubItems("Pasta",20,0));
subItemList.add(new SubItems("Peparoney",20,0));
subItemList.add(new SubItems("asta",40,0));
You are creating new SubItems everytime you start your SubMenuActivity. You need to save these subItems permanently for every category.
One of the way is to create a Category POJO having List<SubItems> and initialize them at the MainMenu Activity and pass that Category POJO on itemClick.
Take an array of Boolean type with size equal to the size of your main menu items.
Initialise all values to false.
And whenever you select any main menu, if it opens, set that position in your array as true and if it closes, set the place as false.
On bindViewHolder add a condition like if the array value is true, show the sub menu else hide the sub menu
And you can also store this array in database to retain the value if you need it even after you kill your application.
Let me know if you need help with the code.
I am trying to list items on my recycler view using the grid layout manager with two columns.
However, the first row of the grid seems to be having width issues.
When I scroll down until the first row is invisible, then scrolled back up, it will fix itself and show the correct width.
My items are retrieved from themoviedb API,
adapter.setMovieWrapper(body); // body contains data from the API
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.setAdapter(adapter);
Here is the layout code for the grid item:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="2dp"
android:orientation="vertical">
<ImageView
android:id="#+id/movie_thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:contentDescription="#string/movie_thumbnail_description"/>
<TextView
android:background="#drawable/bg_shade"
android:id="#+id/movie_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:textColor="#FFF"
android:ellipsize="end"
android:padding="8dp"/>
</FrameLayout>
Here is the adapter code:
public class MovieGridAdapter extends RecyclerView.Adapter<MovieGridItemViewHolder> {
MovieWrapper movieWrapper;
private Context context;
public MovieGridAdapter(Context context) {
this.context = context;
}
public MovieWrapper getMovieWrapper() {
return movieWrapper;
}
public void setMovieWrapper(MovieWrapper movieWrapper) {
this.movieWrapper = movieWrapper;
this.notifyDataSetChanged();
}
#Override
public MovieGridItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MovieGridItemViewHolder(
LayoutInflater.from(context)
.inflate(R.layout.movie_grid_item, parent, false)
);
}
#Override
public void onBindViewHolder(MovieGridItemViewHolder holder, int position) {
Movie movie = movieWrapper.getResults().get(position);
Uri uri = Uri.parse("https://image.tmdb.org/t/p/w185");
uri = uri.buildUpon().appendEncodedPath(movie.getPosterPath()).build();
Picasso.with(context)
.load(uri)
.into(holder.getThumbnail());
holder.getTitle().setText(movie.getTitle());
}
#Override
public int getItemCount() {
if (movieWrapper != null && movieWrapper.getResults() != null) {
return movieWrapper.getResults().size();
}
return 0;
}
}
and here is the activity that host the recycler view XML layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.adityapurwa.popularmovies.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/movie_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
Anyone know what is causing this issue and how to resolve it?
Thanks!
There seems to be an issue with my ConstraintLayout hosting the RecyclerView, I changed it to FrameLayout and the issue disappeared. Even though the layout and the RecyclerView both have its dimension set to match_parent, somehow it fails to detect the dimension.
Note: Changing the dimension to be absolute (e.g 100dp) also solved the problem, however it makes the layout unresponsive.
I have used FrameLayout around Recyclerview but did not work, I used this work around solution like this, for first 2 items i added dummy items and hidden the views, this worked for me , although this is not a perfect solution
I'm trying to create a curved layout to list some elements and adapt it to the android wear, but the Google Android Developers page about it is very unclear. Did anyone manage to create a curved layout?
This is the Google Developers that I'm talking about:
https://developer.android.com/training/wearables/ui/lists.html#creating
If you have any tips to realize it, I'd be glad to see it.
I hope you figured it out by now, but if not, here are the steps with some example code:
in your round\activity_main.xml use android.support.wear.widget.WearableRecyclerView as the root/main element:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.WearableRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rv_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- ... -->
</android.support.wear.widget.WearableRecyclerView>
Then on the MainActivity extend WearableActivity:
import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.support.wear.widget.WearableRecyclerView
public class MainActivity extends WearableActivity {
private android.support.wear.widget.WearableRecyclerView wearableRecyclerView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wearableRecyclerView = findViewById(R.id.rv_test);
wearableRecyclerView.setLayoutManager(new WearableLinearLayoutManager(MainActivity.this));
wearableRecyclerView.setEdgeItemsCenteringEnabled(true);
}
...
}
You need to make sure you are using the right import for WearableRecyclerView (android.support.wear.widget.WearableRecyclerView) on both the xml and java
Next (which isn't explained in the Developers webpage is creating a ViewHolder:
1 - First create an xml layout (here called circle_view.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/close_button"
android:id="#+id/circledImageView"/>
</LinearLayout>
2 - Then create a java class (here called TestHolder.java) and extended it to RecyclerView.ViewHolder:
public class TestHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
public TestHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.circledImageView);
}
}
Then back in the MainActivity.java add the following below the wearableRecyclerView.setEdgeItemsCenteringEnabled(true); inside onCreate:
RecyclerView.Adapter<TestHolder> testHolderAdapter = new RecyclerView.Adapter<TestHolder>() {
#Override
public TestHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.circle_view, parent, false);
return new TestHolder(view);
}
#Override
public void onBindViewHolder(TestHolder holder, int position) {
}
#Override
public int getItemCount() {
//change return number to desired number of items or an array size or list length
return 5;
}
};
wearableRecyclerView.setAdapter(testHolderAdapter);
Thats all, run your app in a rounded wear emulator or on a round/round-with-chin watch and enjoy the curvy list