i couldn't find whats wrong with my code.. RecyclerView not showing up when i run my app..
Thanks in advance.
Main Activity onCreate Method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
toolbar=(Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavFragment drawerFragment = (NavFragment)
getSupportFragmentManager().findFragmentById(R.id.nav_frag);
drawerFragment.setUp(R.id.nav_frag, (DrawerLayout) findViewById(R.id.drawerLayout), toolbar);
listEvents= new ArrayList<>();
Parse.enableLocalDatastore(this);
Parse.initialize(this, "xxxxxxx", "xxxxxxxx");
//TEST PARSE
final TextView t =(TextView) findViewById(R.id.textView);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Test");
//query.whereEqualTo("playerName", "Dan Stemkoski");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> eventList, ParseException e) {
if (e == null) {
Log.d("Events", "Retrieved " + eventList.size() + " Events");
for (int i = 0; i < eventList.size(); i++) {
Events events = new Events();
events.setTitle((String) eventList.get(i).get("teststr"));
events.setId(String.valueOf( eventList.get(i).get("id")));
listEvents.add(events);
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
eventAdapter= new EventAdapter(this);
eventAdapter.setEventList(listEvents);
eventsList=(RecyclerView)findViewById(R.id.list_events);
eventsList.setLayoutManager(new LinearLayoutManager(this));
eventsList.setAdapter(eventAdapter);
}
Event Adapter for the RecylerView :
public class EventAdapter extends RecyclerView.Adapter<EventAdapter.ViewHolderEvents> {
private LayoutInflater layoutInflater;
private ArrayList<Events> listEvents= new ArrayList<>();
public EventAdapter(Context context){
layoutInflater=LayoutInflater.from(context);
}
public void setEventList(ArrayList<Events> listEvents){
this.listEvents=listEvents;
notifyItemRangeChanged(0,listEvents.size());
}
#Override
public ViewHolderEvents onCreateViewHolder(ViewGroup viewGroup, int i) {
View view=layoutInflater.inflate(R.layout.custom_event_row,viewGroup,false);
ViewHolderEvents viewHolder =new ViewHolderEvents(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolderEvents viewHolderEvents, int i) {
Events currentEvent= listEvents.get(i);
viewHolderEvents.title.setText(currentEvent.getTitle());
viewHolderEvents.id.setText(currentEvent.getId());
}
#Override
public int getItemCount() {
return listEvents.size();
}
static class ViewHolderEvents extends RecyclerView.ViewHolder{
private TextView title;
private TextView id;
public ViewHolderEvents(View itemView) {
super(itemView);
title=(TextView)itemView.findViewById(R.id.title);
id=(TextView)itemView.findViewById(R.id.description);
}
}
}
XML Layout which is been set as Layout for the RecylerView: activity_main_appbar :
<LinearLayout 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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/list_events"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<fragment
android:id="#+id/nav_frag"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_nav"
android:name="com.squaredbytes.eventlane.NavFragment"
tools:layout="#layout/fragment_nav" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Try to create and set your EventAdapter into your FindCallback.
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> eventList, ParseException e) {
if (e == null) {
Log.d("Events", "Retrieved " + eventList.size() + " Events");
for (int i = 0; i < eventList.size(); i++) {
Events events = new Events();
events.setTitle((String) eventList.get(i).get("teststr"));
events.setId(String.valueOf( eventList.get(i).get("id")));
listEvents.add(events);
}
eventAdapter = new EventAdapter(this);
eventAdapter.setEventList(listEvents);
eventsList.setAdapter(eventAdapter);
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
Related
During the initialisation of the BottomView part of it appears in the bottom of the screen, when I'm trying to drag it by finger, it appears and then immediately disappear. How to fix it? Or how to make that all items of BottomSheet would appear in the screen, after the FloatActionButton is clicked?
Video of BottomSheet: https://www.youtube.com/watch?v=58bhlc-KfYA&feature=youtu.be
code of Activity:
public class FirstscreenActivity extends AppCompatActivity implements RecyclerItemClickListener.OnItemClickListener,
ItemAdapter.ItemListener {
private BottomSheetDialog mBottomSheetDialog;
BottomSheetBehavior behavior;
private ItemAdapter mAdapterItem;
private FloatingActionButton floatButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front);
mList = (RecyclerView) findViewById(R.id.list);
mList.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mList.addOnItemTouchListener(new RecyclerItemClickListener(this, this));
mList.setLayoutManager(mLayoutManager);
floatButton = (FloatingActionButton) findViewById(R.id.float_button);
floatButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showBottomSheetDialog();
}
});
View bottomSheet = findViewById(R.id.bottom_sheet;
behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
// React to state change
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
// React to dragging events
}
});
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapterItem = new ItemAdapter(createItems(), this);
recyclerView.setAdapter(mAdapterItem);
}
#Override
protected void onResume() {
super.onResume();
RecyclerViewAdapter adapter = (RecyclerViewAdapter) mList.getAdapter();
adapter.notifyDataSetChanged();
}
private void showBottomSheetDialog() {
if (behavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
mBottomSheetDialog = new BottomSheetDialog(this);
View view = getLayoutInflater().inflate(R.layout.sheet, null);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new ItemAdapter(createItems(), new ItemAdapter.ItemListener() {
#Override
public void onItemClick(Item item) {
if (mBottomSheetDialog != null) {
mBottomSheetDialog.dismiss();
}
}
}));
mBottomSheetDialog.setContentView(view);
mBottomSheetDialog.show();
mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
mBottomSheetDialog = null;
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
mAdapterItem.setListener(null);
}
public List<Item> createItems() {
ArrayList<Item> items = new ArrayList<>();
items.add(new Item(R.drawable.camera, "from new shoots"));
items.add(new Item(R.drawable.folder_multiple_image, "from ready images"));
return items;
}
#Override
public void onItemClick(Item item) {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
RecyclerView Adapter inside the BottomSheet:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private List<Item> mItems;
private ItemListener mListener;
public ItemAdapter(List<Item> items, ItemListener listener) {
mItems = items;
mListener = listener;
}
public void setListener(ItemListener listener) {
mListener = listener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter, parent, false));
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setData(mItems.get(position));
}
#Override
public int getItemCount() {
return mItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView imageView;
public TextView textView;
public Item item;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
textView = (TextView) itemView.findViewById(R.id.textView);
}
public void setData(Item item) {
this.item = item;
imageView.setImageResource(item.getDrawableResource());
textView.setText(item.getTitle());
}
#Override
public void onClick(View v) {
if (mListener != null) {
mListener.onItemClick(item);
}
}
}
public interface ItemListener {
void onItemClick(Item item);
}
}
xml of Activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#118b0a"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/my_toolbar" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/float_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:src="#drawable/add_white" />
<android.support.design.widget.CoordinatorLayout
android:layout_width="0dp"
android:layout_height="0dp">
<LinearLayout
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:gravity="center"
android:orientation="vertical"
app:layout_behavior="#string/bottom_sheet_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:background="#fff" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
xml of RecyclerView item inside the BottomSheet:
<?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="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="16dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:gravity="center_vertical"
android:textColor="#787878"
android:textSize="22sp" />
</LinearLayout>
These two lines of code solved my problem:
mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
I am having problems in displaying cards dynamically from a recycler view. Here's my code.
CardActivity.java
public class CardActivity extends AppCompatActivity {
CardAdapter mAdapter;
RecyclerView mRecyclerView;
ArrayList<CardModel> data = new ArrayList<>();
public static String IMGS[] = {
"https://scontent.fmnl4-2.fna.fbcdn.net/v/t1.0-9/12540788_486126334923939_641652950626105372_n.jpg?oh=520090fa887ded912ddb7086fc69fc93&oe=57A04969",
"https://scontent.fmnl4-2.fna.fbcdn.net/t31.0-8/12973436_521081094761796_6679453535369186441_o.jpg",
"https://scontent.fmnl4-2.fna.fbcdn.net/v/t1.0-9/12191632_465602330309673_5460380145671805117_n.jpg?oh=51811b46395fee6e4bdb5394e6725591&oe=57D35DC3",
"https://scontent.fmnl4-2.fna.fbcdn.net/v/t1.0-9/10628472_397420133794560_5446805358922772021_n.jpg?oh=f23ab8761e05b2a50d0d0c9dec4d365b&oe=57DBF1CC",
"https://scontent.fmnl4-2.fna.fbcdn.net/t31.0-8/10697227_314766035393304_2937143993369666506_o.jpg",
"https://scontent.fmnl4-2.fna.fbcdn.net/v/t1.0-9/13133194_10154674163886840_3764712620850385571_n.jpg?oh=252cedf88040188f12ce99c29f3dd47e&oe=57DD7474"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card);
for (int i = 0; i < IMGS.length; i++) {
CardModel cardModel = new CardModel();
cardModel.setTitle("Card Title: " + i);
cardModel.setDescription("This is a card description");
cardModel.setUrl(IMGS[i]);
data.add(cardModel);
}
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Cards");
mRecyclerView = (RecyclerView) findViewById(R.id.cards);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CardActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setHasFixedSize(true);
mAdapter = new CardAdapter(CardActivity.this, data);
mRecyclerView.setAdapter(mAdapter);
}
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}
}
CardModel.java
public class CardModel {
String url,title,description;
public CardModel() {
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
CardAdapter.java
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardHolder> {
private LayoutInflater inflater;
Context context;
List<CardModel> data = new ArrayList<>();
public CardAdapter(Context context, List<CardModel> data){
this.context = context;
this.data = data;
inflater = LayoutInflater.from(context);
}
#Override
public CardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.card_layout,parent,false);
CardHolder cardHolder = new CardHolder(view);
return cardHolder;
}
#Override
public void onBindViewHolder(CardHolder holder, int position) {
holder.cardTitle.setText(data.get(position).getTitle());
holder.cardDescription.setText(data.get(position).getDescription());
Glide.with(context).load(data.get(position).getUrl())
.fitCenter()
.into(holder.cardImage);
}
#Override
public int getItemCount() {
return 0;
}
public static class CardHolder extends RecyclerView.ViewHolder {
ImageView cardImage;
TextView cardTitle;
TextView cardDescription;
public CardHolder(View cardView) {
super(cardView);
cardImage = (ImageView) cardView.findViewById(R.id.card_image);
cardTitle = (TextView) cardView.findViewById(R.id.card_title);
cardDescription = (TextView) cardView.findViewById(R.id.card_description);
}
}
}
activity_card.xml
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.braudy.android.mesasixprofiler.CardActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<include layout="#layout/toolbar"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/cards"
android:layout_width="match_parent"
android:layout_height="fill_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</FrameLayout>
card_layout.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"
android:background="#FFFFFF"
android:padding="10dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#ffffff">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:alpha=".7"
android:id="#+id/card_image"
android:background="#ffff66"
android:src="#drawable/img1"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Braudy"
android:paddingLeft="15dp"
android:textColor="#FFFFFF"
android:background="#707070"
android:alpha=".8"
android:textSize="25sp"
android:id="#+id/card_title"
android:layout_alignBottom="#+id/card_image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="This is a description"
android:paddingTop="5dp"
android:paddingLeft="15dp"
android:textColor="#A9A9AF"
android:textSize="15sp"
android:id="#+id/card_description"
android:layout_below="#+id/card_image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Change this
#Override
public int getItemCount() {
return 0;
}
to
#Override
public int getItemCount() {
return data.size();
}
change here-
#Override
public int getItemCount() {
return data.size();
}
You have to declare how much views you want to inflate in your view.
I am trying to pass the Json data in the view pager, its not showing any errors. But it is also not displaying the images in the view pager, am not able to understand my error
JSON:http://www.souqalkhaleejia.com/webapis/banners.php
Banner.java
public class Banner extends Fragment {
ViewPager bannerpager;
ArrayList<Data> bannerdta = new ArrayList<Data>();
BannerAdapter bannerAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View bannerp = inflater.inflate(R.layout.banner, container, false);
bannerpager = (ViewPager) bannerp.findViewById(R.id.bannerpager);
bannerpager.setAdapter(bannerAdapter);
bannerAdapter = new BannerAdapter(bannerdta, getActivity());
loadbanner();
return bannerp;
}
private void loadbanner() {
String bannerurl = "http://www.souqalkhaleejia.com/webapis/banners.php";
JsonObjectRequest bannerreq = new JsonObjectRequest(Request.Method.GET, bannerurl, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray banners = response.getJSONArray("banners");
for (int i = 0; i < banners.length(); i++) {
JSONObject banner1 = banners.getJSONObject(i);
Data banndata = new Data();
banndata.setBannerimages(banner1.getString("image"));
bannerdta.add(banndata);
}
} catch (JSONException e) {
e.printStackTrace();
}
bannerAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "" + error, Toast.LENGTH_SHORT).show();
}
});
AppController.getInstance().addToRequestQueue(bannerreq);
}
}
Adapter.java
public class BannerAdapter extends PagerAdapter {
Context cntx;
private ArrayList<Data> blist;
private LayoutInflater binflater;
public BannerAdapter(ArrayList<Data> blist, Context cntx) {
this.blist = blist;
this.cntx = cntx;
binflater= (LayoutInflater) cntx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return blist.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return object==view;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View view=binflater.inflate(R.layout.banner_layout,container,false);
NetworkImageView bannerimage= (NetworkImageView) view.findViewById(R.id.bannerimage);
Data bannerdata=blist.get(position);
ImageLoader imageLoader=AppController.getInstance().getImageLoader();
bannerimage.setImageUrl(bannerdata.getBannerimages(),imageLoader);
view.setTag(bannerdata);
container.addView(view);
Log.i("Banner", "instantiateItem() [position: " + position + "]" + " childCount:" + container.getChildCount());
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
Log.i("Banner", "destroyItem() [position: " + position + "]" + " childCount:" + container.getChildCount());
}
#Override
public int getItemPosition(Object object) {
Data data= (Data) ((View) object).getTag();
int position=blist.indexOf(data);
if(position>=0){
return position;
}else {
return POSITION_NONE;
}
}
}
i am including the viewpager from another class to the home page
Home.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/hmebar"
layout="#layout/toolbar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
>
<include layout="#layout/banner"/>
</FrameLayout>
</LinearLayout>
<ListView
android:id="#+id/nav_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#drawable/hover"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
banner.xml(i had wrote viewpager xml here and included in the Home layout)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/bannerpager"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</LinearLayout>
banner_layout.xml(Single viewpager is the image)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.android.volley.toolbox.NetworkImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bannerimage"/>
</LinearLayout>
You not update your bannerdta to bannerAdapter, so bannerAdapter.notifyDataSetChanged() will not work.
you can add method updateData(Data banndata) in BannerAdapter.class
pubilc void updateData(Data banndata) {
blist.add(banndata);
notifyDataSetChanged();
}
and after call network request successfull, usebannerAdapter.updateData(banndata)
I am trying to create a swipeable CardView list inside a RecyclerView. However, none of the string values appear in the CardViews when I run the app.
This is my MainActivity class:
public class MainActivity extends Activity {
private CardViewAdapter mAdapter;
Button periodicbutton;
#Override
protected void onCreate(Bundle savedInstanceState) {
periodicbutton = (Button) findViewById(R.id.periodbutton);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] values = new String[] {"Quantitative Chemistry", "Atomic Structure", "Periodicity",
"Bonding", "Energetics","Kinetics","Equilibrium","Acids & Bases","Oxidation & Reduction","Organic Chemistry"};
final ArrayList<String> mItems = new ArrayList<>();
for (int i = 0; i < values.length; i++) {
mItems.add(values[i]);
}
OnItemTouchListener itemTouchListener = new OnItemTouchListener() {
#Override
public void onCardViewTap(View view, int position) {
Toast.makeText(MainActivity.this, "Swipe me", Toast.LENGTH_SHORT).show();
}
};
mAdapter = new CardViewAdapter(mItems, itemTouchListener);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
recyclerView.setHasFixedSize(false);
SwipeableRecyclerViewTouchListener swipeTouchListener =
new SwipeableRecyclerViewTouchListener(recyclerView,
new SwipeableRecyclerViewTouchListener.SwipeListener() {
#Override
public boolean canSwipe(int position) {
return true;
}
#Override
public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
// Toast.makeText(MainActivity.this, mItems.get(position) + " swiped left", Toast.LENGTH_SHORT).show();
mItems.remove(position);
mAdapter.notifyItemRemoved(position);
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
// Toast.makeText(MainActivity.this, mItems.get(position) + " swiped right", Toast.LENGTH_SHORT).show();
mItems.remove(position);
mAdapter.notifyItemRemoved(position);
}
mAdapter.notifyDataSetChanged();
}
});
recyclerView.addOnItemTouchListener(swipeTouchListener);
}
public interface OnItemTouchListener {
/**
* Callback invoked when the user Taps one of the RecyclerView items
*
* #param view the CardView touched
* #param position the index of the item touched in the RecyclerView
*/
void onCardViewTap(View view, int position);
}
public class CardViewAdapter extends RecyclerView.Adapter<CardViewAdapter.ViewHolder> {
private List<String> cards;
private OnItemTouchListener onItemTouchListener;
public CardViewAdapter(List<String> cards, OnItemTouchListener onItemTouchListener) {
this.cards = cards;
this.onItemTouchListener = onItemTouchListener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
}
#Override
public int getItemCount() {
return cards == null ? 0 : cards.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onItemTouchListener.onCardViewTap(v, getPosition());
}
});
}
}
}
}
This is my activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
android:orientation="vertical"
android:scrollbars="vertical">
<android.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="72dp"
android:background="#color/primary">
</android.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
</android.support.v7.widget.RecyclerView>
</LinearLayout>
This is the card_view.xml file I'm trying to tie the string values to:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v7.widget.CardView
android:id="#+id/quantutative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="vertical"
card_view:cardCornerRadius="4dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="54dp"
android:gravity="center_vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:textColor="#color/card_text"
android:textSize="36sp"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
Building a custom view that inflate an xml.
everything works fine except that when a the custom view is selected, all other views inflating the custom view is also selected. Don't really know what the problem is. Snippet is below.
public class DJV_DropDown extends LinearLayout {
private UI_Model ui_models;
private String[] items;
private ArrayAdapter itemAdapter;
private UI_Object ui_object;
public void setUi_object(UI_Object ui_object) {
this.ui_object = ui_object;
}
public UI_Object getUi_object() {
return ui_object;
}
public final void setViewAttribute(UI_Object ui_object) {
ui_models = new AttributeDefiner().AttributeReader(ui_object.getUi_spec(), ui_object.getStepData(), ui_object.getName());
setUi_object(ui_object);
}
public final void setDefaultAttribute() {
if (getUi_object().getUi_spec().getParameterMode().equalsIgnoreCase("entity")) {
getUi_object().setParameterMode(true);
String entityString = getUi_object().getUi_singleField().getEntitySource();
UI_Entity entity = new UI_Entity(entityString);
ArrayList<Entity> entityArrayList = Entity.getAllEntityByName(getUi_object().getContext(), entity.getName());
getUi_object().setEntityObject(entityArrayList);
String keep = "";
for (int entityIndex = 0; entityIndex < entityArrayList.size(); entityIndex++) {
try {
JSONObject jsonObject = new JSONObject(entityArrayList.get(entityIndex).getValue());
keep = jsonObject.optString("::DisplayName::") + ",";
} catch (JSONException e) {
e.printStackTrace();
}
}
if (!keep.trim().isEmpty()) {
items = keep.substring(0, keep.length() - 1).split(",");
textView.setText(items[0]);
} else {
items = new String[0];
}
} else {
items = getUi_object().getUi_singleField().getSourceContent().split(",");
textView.setText(items[0]);
}
}
public final UI_Model getCustomViewAttribute() {
return ui_models;
}
public DJV_DropDown(Context context) {
super(context);
}
public DJV_DropDown(Context context, UI_Object ui_object) {
super(context);
initialise();
setViewAttribute(ui_object);
setDefaultAttribute();
}
TextView textView;
LinearLayout rootLinearLayout;
static int id = 0;
private void initialise() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.djv_dropdown, this);
textView = (TextView) findViewById(R.id.text);
rootLinearLayout = (LinearLayout) findViewById(R.id.root);
rootLinearLayout.setId(id);
id++; //changed the id because i initially thought the problem was a a result of more than one custom view sharing the same id
}
}
xml that is been inflated:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:background="#drawable/drop_down"
android:padding="12dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:orientation="vertical"
android:layout_margin="5dp"
android:gravity="center_vertical"
android:textSize="16dp"
android:text="uyyu"
android:id="#+id/text"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
<ImageView
android:src="#drawable/spinner_down"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>