I am using cardslib (https://github.com/gabrielemariotti/cardslib) to create a project with cards and use picasso (http://square.github.io/picasso/), in order to dynamically create cards and load pictures into them with urls that I retrieve from Parse in a for loop.
The problem I have is that even though I retrieve the correct data from Parse, only one image is loaded in all of the cards. The titles, subtitles etc are loaded correctly for each separate card, only the image is the same everywhere.
My code is :
final ArrayList<Card> cards = new ArrayList<Card>();
final CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this,cards);
mRecyclerView = (CardListView) this.findViewById(R.id.carddemo_largeimage_text);
AnimationAdapter animCardArrayAdapter = new SwingBottomInAnimationAdapter(mCardArrayAdapter);
animCardArrayAdapter.setAbsListView(mRecyclerView);
mRecyclerView.setExternalAdapter(animCardArrayAdapter, mCardArrayAdapter);
ParseQuery<ParseObject> query = ParseQuery.getQuery("places");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> List, ParseException e) {
if (e == null) {
for ( i=0; i<List.size();i++){
id=new String();
id=List.get(i).getString("picture");
System.out.println(i+" "+ id);
MaterialLargeImageCard card =
MaterialLargeImageCard.with(mContext)
.setTextOverImage(List.get(i).getString("place_id"))
.setTitle(List.get(i).getString("subtitle"))
.setSubTitle(List.get(i).getString("description"))
.useDrawableExternal(new MaterialLargeImageCard.DrawableExternal() {
#Override
public void setupInnerViewElements(ViewGroup parent, View viewImage) {
Picasso.with(mContext).setIndicatorsEnabled(true); //only for debug tests
Picasso.with(mContext)
.load(id)
.error(R.drawable.ic_launcher)
.into((ImageView) viewImage);
}
})
.setupSupplementalActions(R.layout.carddemo_native_material_supplemental_actions_large, actions)
.build();
cards.add(card);
mCardArrayAdapter.notifyDataSetChanged();
card.setOnClickListener(new Card.OnCardClickListener() {
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
public void onClick(Card card, View view) {
Toast.makeText(mContext, " Click on ActionArea ", Toast.LENGTH_SHORT).show();
onClickStart();
Intent myIntent = new Intent(MainActivity.this, ParallaxToolbarScrollViewActivity.class);
//yIntent.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(myIntent);
}
});
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
The layouts I use are:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card="http://schemas.android.com/apk/res-auto"
xmlns:pew="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<it.gmariotti.cardslib.library.view.CardListView
android:id="#+id/carddemo_largeimage_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card:list_card_layout_resourceID="#layout/rowcard"
style="#style/card_external"
/></RelativeLayout>
rowcard.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card="http://schemas.android.com/apk/res-auto"
xmlns:pew="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<it.gmariotti.cardslib.library.view.CardViewNative
android:id="#+id/list_cardId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card:card_layout_resourceID="#layout/native_material_largeimage_text_card"
style="#style/card_external"
/></RelativeLayout>
native_material_largeimage_text_card.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
xmlns:pew="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Card visible layout -->
<it.gmariotti.cardslib.library.view.ForegroundLinearLayout
android:id="#+id/card_main_layout"
style="#style/card.native.main_layout_foreground"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<it.gmariotti.cardslib.library.view.component.CardThumbnailView
style="#style/card.native.card_thumbnail_outer_layout"
android:id="#+id/card_thumbnail_layout"
android:layout_width="match_parent"
card:card_thumbnail_layout_resourceID="#layout/native_thumbnail_largematerial"
android:layout_height="match_parent"/>
<!-- Main Content View -->
<FrameLayout
android:id="#+id/card_main_content_layout"
style="#style/card.native.material_large_image_content_outer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</it.gmariotti.cardslib.library.view.ForegroundLinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/greydiv"/>
<ViewStub
android:id="#+id/card_supplemental_actions_vs"
android:inflatedId="#+id/card_supplemental_actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"/></LinearLayout>
And native_thumbnail_largematerial.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/card_material_largeimage_height">
<com.fmsirvent.ParallaxEverywhere.PEWImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_thumbnail_image"
android:transitionName="test"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scaleType="centerCrop"
style="#style/card.native.card_thumbnail_image"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="#dimen/card_thumbnail_image_text_over_padding_right"
android:paddingLeft="#dimen/card_thumbnail_image_text_over_padding_left"
android:paddingTop="#dimen/card_thumbnail_image_text_over_padding_top"
android:paddingBottom="#dimen/card_thumbnail_image_text_over_padding_bottom">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:fontFamily="#string/card_font_fontFamily_image_text_over"
android:id="#+id/card_thumbnail_image_text_over"
style="#style/card_thumbnail_image_text_over_textstyle"
/>
</FrameLayout></FrameLayout>
I use CardsLib and picasso together for almost all my cards. What I recommend you is to make a CustomCard which extends card and receives a ParseObject as a parameter for the card.
It would make your code much simpler as well as you would add your cards to the list this way:
for(ParseObject p : List) {
cards.add(new MyCustomCard(mContext, p));
}
Your custom card would look something like this:
public class MyCustomCard extends Card implements Card.OnCardClickListener {
private static final String LOG_TAG = MyCustomCard.class.getSimpleName();
private ParseObject parseObject;
public MyCustomCard(Context context, ParseObject data) {
super(context, R.layout.my_custom_layout);
this.parseObject = data;
this.setOnClickListener(this);
}
#Override
public void setupInnerViewElements(ViewGroup parent, View view) {
super.setupInnerViewElements(parent, view);
ImageView imageView = (ImageView) view.findViewById(R.id.data_thumbnail);
TextView title = (TextView) view.findViewById(R.id.title);
TextView text = (TextView) view.findViewById(R.id.text);
if(parseObject != null) {
Picasso.with(getContext()).load(parseObject.getString("picture")).fit().into(imageView);
title.setText(parseObject.getString("title"));
text.setText(parseObject.getString("text"));
}
}
#Override
public void onClick(Card card, View view) {
// in case your cards need to click on something. You don't need to
// override onCLick if you don't wish to have click functionality on the cards
}
}
You could even extend the MaterialLargeImageCard and use it's own XML instead of making your own.
I have some custom cards with Picasso examples in a few of my projects on github: check this project for more examples
Related
I’m trying to implement a RecyclerView with horizontal scrolling, so I’m using this a LinearLayoutManager with horizontal orientation. The problem is that I’m populating the RecyclerView using 2 different types of items, with different heights. This is the layout I’m using for the item:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp">
<LinearLayout
android:id="#+id/document_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/ic_rounded"
android:backgroundTint="#color/ms_black_ms_gray"
android:gravity="center"
android:layout_gravity="bottom"
android:padding="5dp"
android:paddingStart="15dp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13sp"
android:singleLine="true"
android:maxWidth="80dp"
tools:text="example_form"/>
<TextView
android:id="#+id/format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13sp" />
…
</LinearLayout>
<android.support.v7.widget.CardView
android:id="#+id/image_view"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_gravity="bottom"
app:cardCornerRadius="25dp"
app:cardElevation="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/preview_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
…
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is the layout that contains the RecyclerView, which is basically like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="14dp"
android:paddingEnd="14dp">
<ImageView
android:id="#+id/attach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="bottom"
android:layout_marginBottom="19dp"
android:visibility="visible"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:padding="3dp"
android:foreground="#drawable/ic_rounded_stroke"
android:foregroundTint="#color/white">
<android.support.constraint.ConstraintLayout
android:id="#+id/chatEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ic_rounded"
android:foreground="#drawable/ic_rounded_stroke"
android:padding="6dp"
android:visibility="visible">
<EditText
android:id="#+id/editText"
android:textSize="17sp"
android:textColor="#121212"
android:letterSpacing="-0.02"
android:lineSpacingExtra="0sp"
android:padding="10dp"
android:paddingStart="15dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="5"
android:hint="#string/chat_hint"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2500"
android:background="#null"
app:layout_constraintRight_toLeftOf="#id/buttonsContainer"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/send"
android:layout_gravity="bottom"
android:visibility="visible"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:paddingTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ffffff"
android:letterSpacing="-0.02"
android:gravity="center_horizontal"
android:text="#string/send"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/filesList"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:paddingTop="5dp"
android:paddingEnd="5dp"
android:visibility="gone"
app:layout_constraintRight_toLeftOf="#id/send"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
</LinearLayout>
I’m using a single ViewHolder, I just change the visibility of the 2 child views.
The result I expect to get is this one:
But what I’m getting is this; the CardView being cut in half, using the height of the second type of item:
I saw this post, which is similar to my problem. It recommends using Google’s Flexbox. So, I tried to implement FlexboxLayoutManager:
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getContext());
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setFlexWrap(FlexWrap.NOWRAP);
I’m using row direction and It is showing items on next lines if it does not fit in single line. So, I also added No_wrap. And now it is showing items in a single line but do not provide scrolling. Also in this case it tries to fit all items in a single line by decreasing width of items.
I also played with the flex box sample app, but I couldn’t get the result I want.
Is there a way I can achieve horizontal scrolling with the Flexbox integrated with RecyclerView? Or should I use a different approach?
Thanks
EDIT
Thanks for the tips and everything, but it is not solving it. So, I stripped down the code to bare minimum to reproduce this.
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1;
private RecyclerView recyclerView;
private FilesAdapter filesAdapter;
private List<File> filesList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager filesLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(filesLayoutManager);
filesAdapter = new FilesAdapter(filesList);
recyclerView.setAdapter(filesAdapter);
ImageView attach = findViewById(R.id.attach);
attach.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent,"Select Files"), REQUEST_CODE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
try {
if (data != null) {
List<File> uriList = new ArrayList<>();
if (data.getClipData() != null) { // Multiple files
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
Uri uri = data.getClipData().getItemAt(i).getUri();
Pair<Boolean, File> isValid = isFileValid(uri);
if (isValid.first) {
uriList.add(isValid.second);
}
}
} else { // Single file
Uri uri = data.getData();
Pair<Boolean, File> isValid = isFileValid(uri);
if (isValid.first) {
uriList.add(isValid.second);
}
}
if (uriList.size() > 0) {
for (File file : uriList) {
filesList.add(filesList.size(), file);
filesAdapter.notifyItemInserted(filesList.size());
}
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private Pair<Boolean, File> isFileValid(Uri uri) throws NullPointerException {
Pair<Boolean, File> defaultResponse = Pair.create(false, null);
Cursor c = getContentResolver().query(uri, null, null, null, null);
if (c != null) {
c.moveToFirst();
String filename = c.getString(c.getColumnIndex(OpenableColumns.DISPLAY_NAME));
if (isSupported(filename)) {
c.close();
return Pair.create(true, new File(StringUtils.endsWithIgnoreCase(filename, ".pdf") ? DOCUMENT : IMAGE));
} else {
Toast.makeText(this, "File format not supported", Toast.LENGTH_SHORT).show();
c.close();
return defaultResponse;
}
}
return defaultResponse;
}
private boolean isSupported(String filename) {
String[] supportedFormats = { ".pdf", ".jpg", ".gif", ".png" };
for (String format : supportedFormats) {
if (StringUtils.endsWithIgnoreCase(filename, format)) {
return true;
}
}
return false;
}
}
Main activity layout:
<?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"
android:gravity="bottom"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/attach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="19dp"
android:padding="10dp"
android:src="#drawable/ic_attach" />
</LinearLayout>
File:
public class File {
public enum Type {
DOCUMENT,
IMAGE
}
private Type type;
public File(Type type) {
this.type = type;
}
public Type getType() {
return type;
}
}
File Adapter:
public class FilesAdapter extends RecyclerView.Adapter<FilesAdapter.BaseViewHolder> {
private List<File> files;
public FilesAdapter(List<File> files) {
this.files = files;
}
#NonNull
#Override
public FilesAdapter.BaseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(viewType == 0 ? R.layout.document_item : R.layout.image_item, parent, false);
if (viewType == 0) {
return new DocumentViewHolder(view);
} else {
return new ImageViewHolder(view);
}
}
#Override
public void onBindViewHolder(#NonNull FilesAdapter.BaseViewHolder viewHolder, int position) {
viewHolder.bind(files.get(position));
}
#Override
public int getItemViewType(int position) {
if (files.get(position).getType() == File.Type.DOCUMENT) {
return 0;
} else {
return 1;
}
}
#Override
public int getItemCount() {
return files.size();
}
abstract static class BaseViewHolder extends RecyclerView.ViewHolder {
public BaseViewHolder(#NonNull View itemView) {
super(itemView);
}
abstract void bind(File file);
}
static class ImageViewHolder extends BaseViewHolder {
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
}
#Override
void bind(File file) { }
}
static class DocumentViewHolder extends BaseViewHolder {
public DocumentViewHolder(#NonNull View itemView) {
super(itemView);
}
public void bind(File file) { }
}
}
document item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="40dp"
android:background="#drawable/ic_rounded"
android:backgroundTint="#888888"
android:layout_margin="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="PDF"
android:textColor="#android:color/white"/>
</LinearLayout>
image item:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_margin="5dp"
app:cardBackgroundColor="#000000"
app:cardCornerRadius="10dp"
app:cardElevation="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="IMAGE"
android:textColor="#android:color/white"/>
</androidx.cardview.widget.CardView>
if I select an image first, and the several pdfs, it works fine:
But if I first select 3 pdfs, and then an image, this happens:
Any idea how to solve this?
I had a similar issue in another project and i solved it by using the Google library FlexboxLayoutManager.
Get the latest FlexboxLayoutManager Library (https://github.com/google/flexbox-layout) and add it into your grandle dependencies (implementation 'com.google.android:flexbox:2.0.1')
In your Activity add the below lines of code: FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setFlexWrap(FlexWrap.NOWRAP);
recyclerView.setLayoutManager(layoutManager);
To make FlexboxLayoutManager work with horizontal scroll add the below code in your adapter (FilesAdapter) in BaseViewHolder class: abstract static class BaseViewHolder extends RecyclerView.ViewHolder {
public BaseViewHolder(#NonNull View itemView) {
super(itemView);
ViewGroup.LayoutParams lp = itemView.getLayoutParams();
if (lp instanceof FlexboxLayoutManager.LayoutParams) {
FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
flexboxLp.setFlexShrink(0.0f);
flexboxLp.setAlignSelf(AlignItems.FLEX_START); //this will align each itemView on Top or use AlignItems.FLEX_END to align it at Bottom
}
}
abstract void bind(File file);
}
In case it helps anyone else, Kotlin version of MariosP's answer with minor refactors below, but 100% kudos to #MariosP. His answer saved the day for us!
RecyclerView setup (this was from a fragment, called in onViewCreated):
private fun setupRecyclerView() {
val flexBoxLayoutManager = FlexboxLayoutManager(requireContext(), FlexDirection.ROW, FlexWrap.NOWRAP)
with(recycler_view) {
layoutManager = flexBoxLayoutManager
adapter = myAdapter
}
}
Adapter setup:
var items : List<Item>
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bindItem(items[position])
}
In the ViewHolder:
class MyViewHolder(private val itemView: View): RecyclerView.ViewHolder(itemView) {
fun bindItem(item: Item) {
// Do things with item
updateLayoutParamsToAllowHorizontalScrolling()
}
private fun updateLayoutParamsToAllowHorizontalScrolling() {
(itemView.layoutParams as? FlexboxLayoutManager.LayoutParams)?.let {
it.flexShrink = 0.0f
it.alignSelf = AlignItems.FLEX_START
}
}
}
try this for your RecyclerView:
android:layout_height="wrap_content"
Since the XML file that contains your RecyclerView is not complete here I cannot be sure but if your RecyclerView is inside another parent view that is limiting it, then i guess using wrap_content as the height for RecyclerView plus some tweaks should solve it.
Also, note that you are limiting you RecyclerView to the bottom of "editText" from top side so that may be preventing your RecyclerView from expanding too.
All you have to do, is to set recyclerview's height to the height of the biggest item, in your case the image item.
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="80dp" />
The reason Image is getting cropped when you choose pdf files first is because the height of recycleView is 40dp which is the height of pdf item. When you try to add a new item without modifying the existing ones, recycleView height remains the same i.e. 40dp. To enforce a minimum height of 80dp (which is the current height of the image layout), we can use minHeight as follows:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="80dp"
tools:listitem="#layout/document_item"
/>
You can also modify your pdf item layout to align the pdfs center_vertically with image items as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_margin="5dp">
<TextView
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/ic_round"
android:backgroundTint="#888888"
android:text="PDF"
android:textColor="#android:color/white"/>
</LinearLayout>
Cheers :)
First, I think your main layout is a bit overcomplicated. You could do the whole thing in a single ConstraintLayout (if you need framed background around specific items, I recommend to use pure View instances layed out using Barriers and Guidelines - see https://medium.com/better-programming/essential-components-of-constraintlayout-7f4026a1eb87)
Another addition and/or improvement would be to not use right/left constraints, rather start/end. This prepares your layout for RTL display too.
Also, I highly recommend to use separate layout files and ViewHolders for distinct items in a RecyclerView.
As others pointed out in comments, your RecyclerView is layed out using match_parent which can in turn crop your view. You may want to set this wrap_content.
In the meanwhile, you may also want to update dependencies to use Android Jetpack and ditch support libraries.
I have created a study helper app for android. I use a custom array adapter that has a checkbox and a textview in each row. Some of my listviews are populated using this custom adapter. My listviews are used to navigate to other activities, and have onItemClick listeners.
On the android studio emulator, everything works fine. However, I exported the apk and installed it on my phone, and the listviews using my custom adapter no longer did anything on click.
Here is my custom adapter:
public class CheckboxAdapter extends BaseAdapter {
private Context context;
ArrayList<String[]> list = new ArrayList<>();
Boolean completed[];
public CheckboxAdapter(ArrayList<String[]> list,Context context) {
super();
this.context = context;
this.list = list;
completed = new Boolean[list.size()];
for (int i = 0; i < completed.length; i++){
if(list.get(i)[1].equalsIgnoreCase("true")){
completed[i] = true;
} else {
completed[i] = false;
}
}
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
// Class to temporarily store the textview and checkbox in each row
public class ViewHolder {
public TextView nametext;
public CheckBox tick;
}
// Override getView method to set custom row layout
// Use viewHolder to get the textview and checkbox then inflate it
// Set text to passed in list of string data
// Set checkbox to passed in array of booleans, then disable the checkbox
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view = null;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (view == null) {
view = new ViewHolder();
convertView = inflater.inflate( R.layout.checkbox_adapter, null);
view.nametext = (TextView) convertView.findViewById(R.id.adaptertextview);
view.tick = (CheckBox)convertView.findViewById(R.id.adaptercheckbox);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
view.tick.setTag(position);
view.nametext.setText(list.get(position)[0]);
view.tick.setChecked(completed[position]);
view.tick.setEnabled(false);
return convertView;
}
}
And its XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/adaptercheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/adaptertextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Here is an example of a listview using the custom adapter, and its onclick function:
ArrayList<String[]> content = new ArrayList<String[]>();
content.add(new String[] {"Lesson 1 Highest Mark: " + highest[0], completed[0]});
content.add(new String[] {"Lesson 2 Highest Mark: " + highest[1], completed[1]});
content.add(new String[] {"Lesson 3 Highest Mark: " + highest[2], completed[2]});
content.add(new String[] {"Lesson 4 Highest Mark: " + highest[3], completed[3]});
content.add(new String[] {"Lesson 5 Highest Mark: " + highest[4], completed[4]});
content.add(new String[] {"Combined Quiz", "false"});
ListView quizList = (ListView) findViewById(R.id.quizListView);
CheckboxAdapter adapter = new CheckboxAdapter(content, this);
quizList.setAdapter(adapter);
quizList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println(parent. getItemAtPosition(position));
switch (position) {
case 0: Intent i0 = new Intent(Quiz.this, Quiz1.class);
startActivity(i0);
break;
case 1: Intent i1 = new Intent(Quiz.this, Quiz2.class);
startActivity(i1);
break;
case 2: Intent i2 = new Intent(Quiz.this, Quiz3.class);
startActivity(i2);
break;
case 3: Intent i3 = new Intent(Quiz.this, Quiz4.class);
startActivity(i3);
break;
case 4: Intent i4 = new Intent(Quiz.this, Quiz5.class);
startActivity(i4);
break;
case 5: Intent i5 = new Intent(Quiz.this, QuizCombined.class);
startActivity(i5);
break;
default: return;
}
}
});
Any ideas why this only works on the android studio emulator? I've tried on two devices and Memu emulator but it didn't work on any of them, only the android studio emulator.
EDIT: the xml for the activity using the above listview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<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=".Quiz">
<TextView
android:id="#+id/quizTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="56dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/quizTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/quizTextView" />
<ListView
android:id="#+id/quizListView"
android:layout_width="368dp"
android:layout_height="493dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/quizTextView2" />
</android.support.constraint.ConstraintLayout>
<include
layout="#layout/app_bar_main_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main_menu"
app:menu="#menu/activity_main_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
EDIT: I copied the project to my laptop and ran it in the emulator there, and it didn't work. Seems like it ONLY works on my pc
remove the duplicate
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"
from the android.support.constraint.ConstraintLayout.
most likely a transparent layout overlaps and prevents clicking...
the inspect view boundaries button is tacked away in the tiles;
think it's also available in the developer settings.
I am working on a WearOS app. I am using a ListView to show a list of strings. Right now, the ListView looks like this:
I want a single row to take up the entire screen. Once a user swipes up, then Row 2 takes up the entire screen. Swipe up again and Row 3 takes up the entire screen, etc. So like this:
I came across this link, which is exactly what I want to do, but the first method doesn't work and the second method suggests a library, but I don't want to use a library for what seems like a pretty simple task. I don't want to use RecyclerView. Thank you for your help.
Here is my XML file for the main activity, which is where the ListView is.
<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout 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:background="#color/dark_grey"
android:padding="#dimen/box_inset_layout_padding"
tools:context=".MainActivity"
tools:deviceIds="wear">
<!-- Change FrameLayout to a RelativeLayour -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/inner_frame_layout_padding"
app:boxedEdges="all">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
</android.support.wear.widget.BoxInsetLayout>
Try the following (The idea here is to programmatically set the height of the textView to the height of the screen):
1) MnnnnnnnActivity.class:----------
public class MnnnnnnnActivity extends AppCompatActivity {
private ListView lv;
private CustomAdapter customAdapter;
private String[] s = new String[10];
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout6);
for (int i = 0; i < 10; i++) {
s[i] = "ROW " + String.valueOf(i + 1);
}
lv = (ListView) findViewById(R.id.lv);
customAdapter = new CustomAdapter(MnnnnnnnActivity.this, s);
lv.setAdapter(customAdapter);
}
public int getScreenHeight() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
}
2) CustomAdapter.class:--------
public class CustomAdapter extends ArrayAdapter<String> {
private String[] s;
private WeakReference<MnnnnnnnActivity> mActivity;
public CustomAdapter(MnnnnnnnActivity activity1, String[] s) {
super(activity1.getApplicationContext(), R.layout.list_view_item, s);
this.s = s;
mActivity = new WeakReference<MnnnnnnnActivity>(activity1);
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(mActivity != null) {
MnnnnnnnActivity activity = mActivity.get();
if (activity != null) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
convertView = inflater.inflate(R.layout.list_view_item, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(s[position]);
holder.tv.setHeight(activity.getScreenHeight());
}
}
return convertView;
}
private class ViewHolder {
TextView tv;
}
}
3) layout6.xml:-----------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv">
</ListView>
</android.support.constraint.ConstraintLayout>
4) list_view_item.xml:----------
<?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">
<TextView
android:id="#+id/tv"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
5) Note: Although this is tested on phone, the same idea can be used to achieve something similar on a wareable. Also, a better approach would be to set the height of the linearLayout (which contains the textView) to the screen height.
6) Output:
I am using the listView widget on android, and in preview list content I chose "checked list" item
Basically it is a list of items and I should be able to check some items and when I do the check mark next to the item becomes visible (it isn't a checkbox, that is the difference between many other checkable lists)
I don't know how to use it, I would like to know at least how I can check some item, that is make the check mark visible, because when I click on an item, it is clickable but nothing happens...
image of listview in simulator
image of listview in editor
here is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:rsb="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#fffefdff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="540dp"
android:weightSum="1"
android:id="#+id/linearLayoutPreferences"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:focusableInTouchMode="false"
android:divider="#ff080808"
android:dividerPadding="#dimen/activity_horizontal_margin"
android:showDividers="middle|beginning|end">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
tools:listitem="#android:layout/simple_list_item_checked"
android:clickable="true"
android:fastScrollAlwaysVisible="false"
android:choiceMode="multipleChoice"
android:contextClickable="false" />
</LinearLayout>
here is my java file
public class Popneighbourhood extends AppCompatActivity {
ListView listNeighbourhood;
String[] neighbourhood = new String[]{
"Alamo Square/NOPA", "Castro/Upper Market", "Central Richmond", "Cole Valley/Ashbury Heights", "Downtown/Civic/Van Ness", "Duboce Triangle",
"Financial District", "Glen Park", "Haight Ashbury", "Hayes Vallez", "Ingleside/SFSU/CCSF", "Inner Richmond",
"Inner Sunset/UCSF", "Jordan Park/Laurel Heights", "Laurel Heights/Presidio", "Lower Haight", "Lower Nob Hill", "Lower Pac Heights",
"Marina/Cow Hollow", "Mission Bay", "Mission District", "Nob Hill", "Noe Valley", "North Beach/Telegraph Hill",
"Oakland North/Temescal", "Pacific Heights"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popneighbourhood);
ActionBar actionBar=getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.logofrontdoor);
listNeighbourhood = (ListView) findViewById(R.id.listView);
//android.R.layout.simple_list_item_1 est une vue disponible de base dans le SDK android,
//Contenant une TextView avec comme identifiant "#android:id/text1"
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Popneighbourhood.this,
android.R.layout.simple_list_item_1, neighbourhood);
listNeighbourhood.setAdapter(adapter);
//adapter code , i just took one of the adapter source from my project, notice the imageView, iv_item_fragment_dashboard_country_list_select, is toggled when you select.
public class DashboardCountryListAdapter extends BaseAdapter {
private Context mContext;
private List<Country> mCountryList = new ArrayList<>();
public DashboardCountryListAdapter(Context context, List<Country> countryList) {
mContext = context;
mCountryList = countryList;
}
#Override
public int getCount() {
return mCountryList.size();
}
#Override
public Object getItem(int position) {
return mCountryList.isEmpty() ? null : mCountryList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_dashboard_country_list, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (!mCountryList.isEmpty()) {
Country country = mCountryList.get(position);
int image = country.getImage();
if (image != -1) {
viewHolder.iv_item_fragment_dashboard_country_list.setImageResource(image);
if (country.getSelected()){
viewHolder.iv_item_fragment_dashboard_country_list_select.setVisibility(View.VISIBLE);
}
else
viewHolder.iv_item_fragment_dashboard_country_list_select.setVisibility(View.GONE);
}
viewHolder.tv_item_fragment_dashboard_country_list.setText(country.getName());
}
return convertView;
}
class ViewHolder {
#Bind(R.id.iv_item_fragment_dashboard_country_list)
ImageView iv_item_fragment_dashboard_country_list;
#Bind(R.id.tv_item_fragment_dashboard_country_list)
TextView tv_item_fragment_dashboard_country_list;
#Bind(R.id.iv_item_fragment_dashboard_country_list_select)
ImageView iv_item_fragment_dashboard_country_list_select;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
public void updateList(List<Country> list) {
mCountryList = list;
notifyDataSetChanged();
}
}
//xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="#+id/iv_item_fragment_dashboard_country_list"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:layout_width="20dp"
android:layout_height="15dp" />
<com.UTU.View.UtuTextView
android:id="#+id/tv_item_fragment_dashboard_country_list"
android:textColor="#color/chic_black"
android:textSize="18sp"
android:gravity="center_vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/iv_item_fragment_dashboard_country_list_select"
android:src="#drawable/icon_check_teal"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:layout_width="13dp"
android:layout_height="10dp" />
</LinearLayout>
I can tell you why the editor looks different from the running app:
In your layout XML the list view has this attribute:
tools:listitem="#android:layout/simple_list_item_checked"
but in your code you have
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Popneighbourhood.this,
android.R.layout.simple_list_item_1, neighbourhood);
so you're using an entirely different layout for the list item.
You need to put R.layout.simple_list_item_checked in the adapter constructor, and you will probably need to change to the constructor that specifies the id of the TextView to use.
I am trying to use Cardslib to create a RecyclerView with custom layout rows that would expand when I click a button on them. I followed customization guide and create both custom layout and custom card class, the cards do appear correctly like this
but I cant figure out how to set clicking event for view elements on this card.
In activity_main.xml, I have a CardRecyclerView:
<it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
card:list_card_layout_resourceID="#layout/my_cardview_layout"
android:id="#+id/my_recyclerview"/>
my_cardview_layout.xml:
<it.gmariotti.cardslib.library.view.CardViewNative xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_cardId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
card:card_layout_resourceID="#layout/my_material_card_layout"
style="#style/card_external"
android:layout_marginTop="12dp" />
my_material_card_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<it.gmariotti.cardslib.library.view.ForegroundLinearLayout
android:id="#+id/card_main_layout"
style="#style/card.native.main_layout_foreground"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="210dp"
android:id="#+id/mycard_image"
android:src="#drawable/sea"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="#+id/mycard_title"
android:paddingTop="24dp"
android:paddingLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Subtitle"
android:id="#+id/mycard_subtitle"
android:paddingLeft="20dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/actions_padding"
android:paddingLeft="#dimen/actions_padding_left"
android:paddingRight="#dimen/actions_padding_left">
<TextView
android:id="#+id/mycard_suppaction1"
android:text="SHARE"
android:background="?android:selectableItemBackground"
android:layout_width="wrap_content"
style="#style/card.native.actions"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/mycard_suppaction2"
android:text="LEARN MORE"
android:background="?android:selectableItemBackground"
android:layout_width="wrap_content"
style="#style/card.native.actions"
android:layout_height="wrap_content"/>
</LinearLayout>
</it.gmariotti.cardslib.library.view.ForegroundLinearLayout>
<FrameLayout
android:id="#+id/card_content_expand_layout"
style="#style/card.native.main_contentExpand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</FrameLayout>
My main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Card> cards = new ArrayList<Card>();
//CardViewNative cardView = (CardViewNative) findViewById(R.id.myMaterialCardView);
for(int i = 0; i<3; i++) {
MyMaterialCard card = new MyMaterialCard(this);
CardExpand expand = new CardExpand(getApplicationContext());
//Set inner title in Expand Area
expand.setTitle(" Hidden content");
card.addCardExpand(expand);
card.setId("" + i);
cards.add(card);
}
CardArrayRecyclerViewAdapter mCardArrayAdapter = new CardArrayRecyclerViewAdapter(this, cards);
//Staggered grid view
CardRecyclerView mRecyclerView = (CardRecyclerView) this.findViewById(R.id.my_recyclerview);
mRecyclerView.setHasFixedSize(false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//Set the empty view
if (mRecyclerView != null) {
mRecyclerView.setAdapter(mCardArrayAdapter);
}
}
my custom card class:
public class MyMaterialCard extends Card {
ImageView mImage;
TextView mTitle;
TextView mSubtitle;
TextView mActionButton1;
TextView mActionButton2;
public MyMaterialCard(Context context) {
super(context, R.layout.my_cardview_layout);
}
public MyMaterialCard(Context context, int innerLayout) {
super(context, innerLayout);
}
#Override
public void setupInnerViewElements(ViewGroup parent, View view) {
mImage = (ImageView) view.findViewById(R.id.mycard_image);
mTitle = (TextView) view.findViewById(R.id.mycard_title);
mSubtitle = (TextView) view.findViewById(R.id.mycard_subtitle);
mActionButton1 = (TextView) view.findViewById(R.id.mycard_suppaction1);
mActionButton2 = (TextView) view.findViewById(R.id.mycard_suppaction2);
mActionButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "action 1", Toast.LENGTH_SHORT).show();
}
});
ViewToClickToExpand viewToClickToExpand =
ViewToClickToExpand.builder()
.setupView(mActionButton2);
setViewToClickToExpand(viewToClickToExpand);
}}
I think the problem maybe the my_cardview_layout.xml, however, if i set card:list_card_layout_resourceID="#layout/my_material_card_layout"
in recyclerview and
public MyMaterialCard(Context context) {
super(context, R.layout.my_material_card_layout);}
they will not appear as cards:
have been working on this for 2 days and still can not get those buttons to work ( tried using Cardslib material card but when add expand, nothing happened). Really appreciate any help, thank you!
On main activity, add function setOnClickListener:
for(int i = 0; i<3; i++) {
MyMaterialCard card4 = new MyMaterialCard(this);
card.setOnClickListener(new Card.OnCardClickListener() {
#Override
public void onClick(Card card, View view) {
Toast.makeText(view.getContext()," Click on ActionArea ",Toast.LENGTH_SHORT).show();
}
});
CardExpand expand = new CardExpand(getApplicationContext());
//Set inner title in Expand Area
expand.setTitle(" Hidden content");
card.addCardExpand(expand);
card.setId("" + i);
cards.add(card4);
}
I think this could help some people, I hope not you xD (1 year, 10 months ago)