I have an app that already many activities and class. Like Profile, Friends, Photos, News Feed, etc. I want to integrate that sliding drawer into my main activities. in the examples, they are just showing array list which is implement onClick. so my question is how to change that array list to become my activities like friends, profile, home and etc.
You can use ListView with custom adapter. to adjust custom adapter you can link in your listView that adapter. and in adapter you can put custom icon
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflator.inflate(R.layout.row, parent, false);
ImageView icon = (ImageView) rowView.findViewById(R.id.row_icon);
TextView title = (TextView) rowView.findViewById(R.id.row_title);
title.setText(proMenu[position]);
String s = proMenu[position];
if(s.equalsIgnoreCase("Home")){
icon.setImageResource(R.drawable.icon_home);
}
else
if(s.equalsIgnoreCase("Best Nearby")){
icon.setImageResource(R.drawable.specialss);
}
Related
I am trying to implement a Newsfeed-type layout with multiple feed items.
The newsfeed item would have a certain layout when collapsed, and this layout would be replaced by an 'exploded' version, when the item is clicked.
I accomplished this by using a ListView of custom items. The custom item XML layout file has a ViewStub which is what I used to change the layout back and forth.
Now, though, I wanted to 'migrate' the layout over to RecyclerView, and to also follow a ViewHolder design pattern.
The latter is what I have tried first, and I'm running into all sorts of problems.
My approach has been as follows:
Get reference to collapsed layout (events_list_item_content) and expanded layout (events_list_item_selected_content);
Get reference to a simple layout resource file to be set as the ViewStub layout (view_stub_layout).
Get ViewStub reference, set its layout (view_stub_layout) inflate, and add the collapsed layout view to this layout (when first creating the feed, all of its items are going to be collapsed).
(After initialisation, when an item is clicked) Remove previous view (layout) from the ViewStubLayout, add the other type of layout.
Here is my custom adapter class:
public class FeedRecyclerAdapter extends BaseAdapter {
public class ViewHolder {
View inflatedViewStub1;
ViewStub viewStub;
LinearLayout viewStubLayout;
LinearLayout listItemContent, listItemContentSelected;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final FeedItem item = feedItems.get(position);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.events_list_item_content_new_container, parent, false);
View view = null;
view = inflater.inflate(R.layout.events_list_item_content, null);
viewHolder.listItemContent = (LinearLayout) view.findViewById(R.id.events_list_item_content);
view = inflater.inflate(R.layout.events_list_item_selected_content, null);
viewHolder.listItemContentSelected = (LinearLayout) view.findViewById(R.id.events_list_item_content_selected);
view = inflater.inflate(R.layout.view_stub_layout, null);
viewHolder.viewStubLayout = (LinearLayout) view.findViewById(R.id.view_stub_layout);
viewHolder.viewStub = (ViewStub) convertView.findViewById(R.id.list_item_feed);
(viewHolder.viewStubLayout).addView(viewHolder.listItemContent);
viewHolder.viewStub.setLayoutResource(R.layout.view_stub_layout);
viewHolder.inflatedViewStub1 = viewHolder.viewStub.inflate();
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();
if (item.getExploded()) {
viewHolder.viewStubLayout.removeAllViews();
viewHolder.viewStubLayout.addView(viewHolder.listItemContentSelected);
} else {
viewHolder.viewStubLayout.removeAllViews();
viewHolder.viewStubLayout.addView(viewHolder.listItemContent);
}
return convertView;
}
However, when testing, the page where the Newsfeed is supposed to appear is blank.
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
stub.setLayoutResource(layoutId);
stub.inflate(); // inflate 1st layout
ll.removeAllViews(); // remove previous view, add 2nd layout
ll.addView(LayoutInflater.from(context).inflate(secondLayoutId, ll, false));
Android ViewStub change layouts programatically
I have custom listview and array adapter with ViewHolder. When click listview item, it expands new layout below. Problem is: unfortunately it is opened for every +9th item in listview.. For example: if item 0 is clicked; 0,9,18th elements opens their expand layouts. Any idea without looking code ?
I have no idea, but this sounds familair with an issue I had. I had a listview with TextView objects and multiple were selected and got typed in the same value.
I had a very, very dirty fix for that, in my custom adapter I'd always make a new View, no matter what:
#Override
public View getView (final int position, View convertView, final ViewGroup parent) {
//if (convertView == null) {
// Inflate the view from the converter
final LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
convertView = layoutInflater.inflate(converter.getLayout(), parent, false);
//}
// Populate the view from the converter
converter.populateInflatedView(convertView, getItem(position));
return convertView;
}
Source can be found here if you want to know what the converter is about.
On a side note, I have created a sort of interface type of thing for a TreeView, however this is in Activity form which uses a ScrollView. So this is not really a ListView type of deal, but might help you. The Tree part can be found on the Tree-link, with an implementation in the subject package.
I got a ListView with a custom Adapter.
In the Adapter a layout is inflated from xml.
All works fine, and I can see the items, until the Screen Orientation is changed.
I know that the Activity is recreated (or resumed) then, and the ListView is recreated too, as well as the Adapter.
But there are no items in the ListView now. The Adapter isn't empty, I use toasts to display the count of items in the Adapter.
I guess there is an inflating problem, because if I use the same Adapter (or an adapter with the same data) to a new ListView nothing is shown as well.
But the most crazy thing I don't understand is, that if I let my getView() method return a simple TextView, all works fine, even after orientation change.
I tried several things, like don't recycle a View so that it is inflated every time, or save the View to the matching Item (from getItem(position) from the Adapter).
I'm grateful for all hints :)
EDIT: so I was asked for some code.
Here is the getView() of my Adaptar
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
final Event event = getItem(position);
if (view == null) {
view = inflater.inflate(R.layout.new_event_item_layout, parent,
false);
view.setTag(R.id.eventDate, view.findViewById(R.id.eventDate));
view.setTag(R.id.eventTime, view.findViewById(R.id.eventTime));
view.setTag(R.id.eventName, view.findViewById(R.id.eventName));
view.setTag(R.id.eventBemerkungen,
view.findViewById(R.id.eventBemerkungen));
view.setTag(R.id.eventIcon, view.findViewById(R.id.eventIcon));
}
((TextView) view.getTag(R.id.eventDate)).setText(event.getDate());
((TextView) view.getTag(R.id.eventTime)).setText(event.getName());
((TextView) view.getTag(R.id.eventName)).setText(event.getTime());
((TextView) view.getTag(R.id.eventBemerkungen)).setText(event
.getDescription());
SquaredImageView icon = (SquaredImageView) view.getTag(R.id.eventIcon);
Picasso.with(context).load(event.getUri())
.placeholder(R.drawable.ic_reload).into(icon);
view.setBackgroundColor(event.getBackgroundColor());
view.setVisibility(View.VISIBLE);
return view;
//return getDummyTextView();
}
public TextView getDummyTextView()
{
TextView tv=new TextView(context);
tv.setText("YOLO BIATCHSES");
tv.setTextColor(Color.WHITE);
return tv;
}
I have had the same exact problem although it wasn't because of orientation change and I found the solution by just setting listView.setAdapter(adapter) again after the recreation of the activity or whatever case you have. I suspect the listview is basically losing the pointer to the adapter.
i'm trying to set textView in custom row view of listView like below but the app crashes returning null pointer exception
TextView txtrow = (TextView) getListView().getChildAt(i).findViewById(R.id.text11);
txtrow.setText("text");
but when i put it under setOnClickListener it works
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView txtrow = (TextView) getListView().getChildAt(i).findViewById(R.id.text11);
txtrow.setText("text");
}
});
You should be doing this inside your ListView's adapter. Whatever data you are using the back your adapter should contain the text you want so that you can set it on the appropriate TextView when you actually create the row view in getView().
It's important to note that getChildAt(index) returns the child at the given index from the ListView's current children. In other words, getChildAt(0) does not necessarily give you the list item at position 0. The user may have scrolled the list so that it now shows data from positions 10 through 15 (for example), in which case getChildAt(0) gives you the view for position 10, not position 0.
To set text in custom row in listview
Please follow the following points
1. custom arrayadapter by extending Arrayadpater (for simple)
2. and overwrite getview as following
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
return rowView;
}
3. and set the adapter to the listview
And for more detial please refer this site
[http://www.vogella.com/tutorials/AndroidListView/article.html][1]
I have to inflate the group of items in a listview and i need to find inflated view anywhere in the program .
May I know how to find the view.
How to set id and get id of inflated listview.
Thanks in Advance
In the Adapter of your list view
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.newlistrow, null);
//Access your inflated layout using vi
TextView sometextview= (TextView) vi.findViewById(R.id.textview); // sometextview
Typically you don't find views in a list view. The adapter behind the list view will pass you the view plus the underlying position of your data that you need to render via the getView method.
To answer your question of how to get or set id of inflated view you should call getId or setId on the inflated view.
http://developer.android.com/reference/android/view/View.html#setId(int)
http://developer.android.com/reference/android/view/View.html#getId()
View myView = LayoutInflater.from(activity).inflate(R.layout.list_item_punch_log, null);
myView.setId(android.R.id.button1);
int id = myView.getId();
However doing this and finding views in a list view is a very dangerous act as it will cause some really funny behaviors on the listview. Maybe you can tell us what you are trying to do and we can guide you from there.