Android + Use ViewFlipper to flip individual items within a ListView? - android

This is probably a stupid question.
I know I can wrap a ListView in ViewFlipper, but can we wrap individual ListView Items in a ViewFlipper? For instance, the latest Twitter app uses a ListView to display Tweets. It also allows you to set settings on individual items by sliding the tweet out of the way exposing the setting option icons below. Is this a custom implementation or do we have the ability to create something similar using ListView and ViewFlipper? Thanks, any advice is appreciated!
I've spent some time on this and got the ListView to display additional content via the ViewFlipper. However, the view only seems to "flip" on the last item in the ListView. How do I get each item to flip when clicked? Here's some code:
row.xml
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flipper" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/toptext" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1"
android:gravity="center_vertical" />
<TextView android:id="#+id/bottomtext" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1"
android:gravity="center_vertical" />
</ViewFlipper>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
ListActivityView.java - extends ListActivity
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// doesn't matter which line is clicked
// only the last item in the ListView displays the bottomText
viewFlipper.showNext();
}
ListingAdapter.java - extends ArrayAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
viewFlipper = (ViewFlipper) v.findViewById(R.id.flipper);
}

If I am getting your question correct - you can use ViewFlipper inside a layout that defines a row in your list and init it the way you like when rendering corresponding view

Below code helps you achieve a requirement for having a view flipper in list view with invidual row flipping
## xml file ##
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/regularLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<ViewFlipper
android:id="#+id/row_item_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Regular Layout"
android:textSize="28sp" />
<Button
android:text="Flip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/front_button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="#+id/backTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Flip row done"
android:textSize="18sp" />
<Button
android:text="Flip back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/back_button" />
</LinearLayout>
</ViewFlipper>
</LinearLayout>
----------
## Java code ##
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
return new ItemViewHolder(itemView);
}
#Override
public void onBindViewHolder(final ItemViewHolder holder, int position) {
final CustomItem data = dataList.get(position);
holder.regularLayout.setVisibility(View.VISIBLE);
holder.swipeLayout.setVisibility(View.GONE);
holder.listItem.setText(data.dataItemValue );
holder.backTextView.setText(data.dataItemValue + " position : "+ position);
holder.viewFlipper.setFlipInterval(0);
holder.viewFlipper.setInAnimation(null);
holder.viewFlipper.setOutAnimation(null);
if(holder.viewFlipper.getDisplayedChild()==0 && data.isFlipON){
holder.viewFlipper.setDisplayedChild(1);
} else if(holder.viewFlipper.getDisplayedChild()==1 && !data.isFlipON){
holder.viewFlipper.setDisplayedChild(0);
}else if(data.isFlipON){
holder.viewFlipper.setDisplayedChild(1);
}else{
holder.viewFlipper.setDisplayedChild(0);
}
holder.frontButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AnimationFactory.flipTransition(holder.viewFlipper, AnimationFactory.FlipDirection.LEFT_RIGHT);
data.isFlipON = true;
}
});
holder.backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
data.isFlipON = false;
AnimationFactory.flipTransition(holder.viewFlipper, AnimationFactory.FlipDirection.LEFT_RIGHT);
}
});
}

Related

Expandable List view with recycler list view as child

As shown in the image, I am able to render the view. But the onChildClick event of Expandable view is not getting triggered.
Also the touch event in the adapter is not responding. My layout details are mentioned below.
The full source code for this can be found in here.
main_activity.xml
<ExpandableListView
android:id="#+id/list_brands"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null"
android:groupIndicator="#null"
android:dividerHeight="5dp" />
item_parent.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="#+id/text_brand"
android:textSize="18dp"
android:text="Text"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image_indicator"
android:src="#drawable/ic_keyboard_arrow_down_black_18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
item_group_child.xml
<android.support.v7.widget.RecyclerViewxmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mobiles"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
item_child.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="#+id/image_mobile"
android:layout_width="70dp"
android:adjustViewBounds="true"
android:layout_height="120dp" />
<TextView
android:id="#+id/text_mobile_model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:id="#+id/text_mobile_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
**My Solution : **
Touch events are directly passed to the UI Elements of the View holder in the Recycler List view. But to give the touch event to the whole layout I have added the transparent button on top of child_item layout and added a click listener. Following is the updated child_item.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/dummy_click"
android:background="#android:color/transparent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:padding="10dp"
android:background="#color/colorGrey"
>
<ImageView
android:id="#+id/img_subcategory"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher"
android:layout_marginTop="10dp"
android:layout_width="180dp"
android:layout_height="90dp"
android:scaleType="fitXY"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sub Category"
android:textColor="#color/colorWhite"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:id="#+id/txt_subcategory"
android:textSize="#dimen/app_text_title"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Theory
ExpandableListView, child click sample:
for click on child, you can handle this way.
getExpandableListView().setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// your code...
}
});
Expandable RecyclerView, useful library and easy item click support:
This blog post by Hugo shows an alternative way without listeners:
http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/
It comes down to adding these lines:
ItemClickSupport.addTo(mRecyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
// do it
}
});
When using this class:
public class ItemClickSupport {...}
And this value in ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="item_click_support" type="id" />
</resources>
Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview?
Since the introduction of ListView, onItemClickListener has been
problematic. The moment you have a click listener for any of the
internal elements the callback would not be triggered, but it wasn't
notified or well documented (if at all) so there was a lot of
confusion and SO questions about it.
OnItemClickListener doesn't work with ListView item containing button
Just add this line into the item views instead of listView itself
android:focusable="false"
Check more detail about this from: Android custom ListView unable to click on items
Practice
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_group_child, parent, false);
childHolder = new ChildHolder();
// Fix listview onclicklistener issue: inner views can not be focusable
childHolder.YOUR_INNER_BUTTON = (YOUR_BUTTON_TYPE) convertView.findViewById(R.id.YOUR_BUTTON_ID);
childHolder.YOUR_INNER_BUTTON.setFocusable(false);
convertView.setTag(childHolder);
}
else {
childHolder = (ChildHolder) convertView.getTag();
}
childHolder.horizontalListView = (RecyclerView) convertView.findViewById(R.id.mobiles);
LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
childHolder.horizontalListView.setLayoutManager(layoutManager);
MobileAdapter horizontalListAdapter = new MobileAdapter(context, brands.get(groupPosition).mobiles);
childHolder.horizontalListView.setAdapter(horizontalListAdapter);
return convertView;
}
Added two lines to your code based on my legacy code:
// Creates a ViewHolder and store references to the children views (collapsed)
holder = new ViewHolderChildren();
holder.textLine = (TextView) convertView.findViewById(R.id.usersTextLine);
holder.subtextLine = (TextView) convertView.findViewById(R.id.usersSubtextLine);
holder.iconLine = (ImageView) convertView.findViewById(R.id.usersIconLine);
holder.buttonLine = (ImageButton) convertView.findViewById(R.id.usersButtonLine);
holder.infoLine = (TextView) convertView.findViewById(R.id.usersInfoLine);
holder.infoLine.setVisibility(TextView.GONE);
holder.userprofileButton = (ImageButton) convertView.findViewById(R.id.usersUserprofileBtn);
holder.buttonLine.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// if gone, set visible and arrow up image
if (holder.infoLine.getVisibility() == TextView.GONE) {
holder.infoLine.setVisibility(TextView.VISIBLE);
holder.buttonLine.setImageLevel(1);
}
// if visible, set gone and arrow down image
else {
holder.infoLine.setVisibility(TextView.GONE);
holder.buttonLine.setImageLevel(0);
}
}
});
//fixed listview onclicklistener bug: inner views can not be focusable
holder.buttonLine.setFocusable(false);
convertView.setTag(holder);

Android ListView DPAD navigation: Why do I have to the click up button twice to scroll up?

I am navigating a simple listView with a remote control, using DPAD_UP to go up & DPAD_DOWN to go down the list. I am not doing anything special beyond the vanilla stuff you see going on below. Each time I click the down button it scrolls to the next row down just fine. However, when going the reverse direction (i.e. clicking the up button) I have to click it twice to scroll up a row except for a select few # of rows which consistently work.
Each time I click the up button the OnItemSelectedListener is fired. For example, I am at position 10, I hit the up button, onItemSelected(pos=10) is fired. I hit the up button again, now the listview scrolls up and onItemSelected(pos=9) is fired. I repeat, no change in listView & onItemSelected(pos=9), etc.
Using Jelly Bean, API Level 17, this is not touchscreen.
Fragment Code:
import android.support.v4.app.Fragment;
import android.widget.ListView;
...
public class MyFragment extends Fragment {
ListView mListView = null;
ChListAdapter mChListAdapter = null;
...
#Override onCreateView(...) {
mListView = (ListView) parentView.findViewById(R.id.chListView);
mChListAdapter = new ChListAdapter()
mListView.setAdapter(mChListAdapter);
mListView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
android.util.Log.e(TAG,"POS: " + position);
}
...
}
...
#Override onResume(...) {
mListView.requestFocus();
}
...
public void setSelectedCh(int chId) { // request from Activity
mListView.setSelection(<private method to get pos from chId>);
}
...
class ChListAdapter extends ArrayAdapter<Stuff> {
public ChListAdapter() {
super(getActivity(), R.layout.row_ch, R.id.row_ch_name,<list in here>);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
TextView txt1 = (TextView) row.findViewById(R.id.row_ch_name);
txt1.setText(...);
TextView txt2 = (TextView) row.findViewById(R.id.row_ch_number);
txt2.setText(...);
ImageView imgView = (ImageView) row.findViewById(R.id.row_ch_image);
imgView.setImageDrawable(logoBitmapDrawable);
return row;
}
}
...
}
FRAGMENT XML:
<LinearLayout...
<LinearLayout...
...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="100"
android:baselineAligned="false"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="3dp"
android:paddingLeft="10dp"
android:paddingTop="3dp"
android:text="#string/asdf" />
<ListView
android:id="#+id/chListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/darker_gray"
android:dividerHeight="1dp" />
</LinearLayout>
...
</LinearLayout>
</LinearLayout>
LISTVIEW ROW XML (/layout/row_ch.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:orientation="horizontal" >
<ImageView
android:id="#+id/row_ch_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/aaa" />
<TextView
android:id="#+id/row_ch_number"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="bbb" />
<TextView
android:id="#+id/row_ch_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:text="ccc" />
</LinearLayout>

How to implement Scroll View with views getting aligned at the top?

I came across this UI and am trying to use it.
In the image what I guess is, the root layout is a Scroll view.
The top view inside the ScrollView is Relative layout and below it is ListView which gets loaded depending on the dates selected. (not sure)
When the view is scrolled up the button present on the top View aligns itself to the top of the parent layout.
And then the rest is scrolled like a listview.
When scrolled down again the top view is visible.
NB: I have tried to apply what I feel like has been used for such effect. But I am not able to align the button of the top view to the parent layout. If needed I can post the code.
Please help me to find what exactly is the logic behind this effect.
Thanks.
<ScrollView 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="com.mypackage.Appointments_Main_ActivityFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/linear"
android:layout_height="wrap_content">
<TextView
android:text="From Date"
android:id="#+id/frm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip" />
<TextView
android:text="To Date"
android:id="#+id/to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip" />
</LinearLayout>
<Button
android:id="#+id/alertbtn"
android:layout_below="#id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/DarkGray"
android:padding="16dp"
android:text="Alerts"
android:textColor="#color/accentforGray" />
<ListView
android:layout_below="#id/alertbtn"
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#B29090" >
</ListView>
</RelativeLayout>
I solved the problem myself.
For getting the same look and feel as mentioned in the image.
I did the following:
1) Defined 3 layouts used in ListView.
list_top_one -> for top view having from date and to date text view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/frmdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="from date"/>
<TextView
android:id="#+id/todate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="to date"/>
</LinearLayout>
b) list_item_in_middle -> for having button like feel
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:id="#+id/llHeader">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_background"
android:text="OK"
android:padding="16dp"/>
</LinearLayout>
c) list_items -> for holding the dynamically loaded value.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFBB00"
android:orientation="vertical" >
<TextView
android:textColor="#000"
android:layout_margin="30dp"
android:id="#+id/button1"
android:layout_gravity="center"
android:text="OK"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2) Main activity layout.
a) Contains a listview
b) And includes a layout that is to be shown as button.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<!-- This LinearLayout's visibility is toggled -->
<include layout="#layout/list_item_in_middle" />
</RelativeLayout>
Reference:
Making a middle element to get stuck in the header (ScrollView/ListView)
3) ListAdapter class
public class ListAdapter extends BaseAdapter {
public Context mContext;
TextView btCurrent;
ArrayList<Integer> newarrays = new ArrayList<>();
ListAdapter(Context context,Integer[] arrval) {
mContext = context;
newarrays.addAll(Arrays.asList(arrval));
}
public void InitializeValues(Integer[] arrval){
newarrays.addAll(Arrays.asList(arrval));
}
#Override
public int getCount() {
return newarrays.size();
}
#Override
public Object getItem(int arg0) {
return newarrays.get(arg0);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(position == 0){
convertView = inflater.inflate(R.layout.list_item_in_middle, null);//Adding btn layout
}else {
convertView = inflater.inflate(R.layout.list_items, null);// Adding other elements
btCurrent = (TextView) convertView.findViewById(R.id.button1);
if ((Integer) getItem(position) == 3) {
btCurrent.setText("Number " + getItem(position) + " is sticky");
} else {
btCurrent.setText("" + getItem(position));
}
}
return convertView;
}
}
4) MainActivity
a) OnScrollListener for attaching the btn like list item on top.
llHeader = (LinearLayout) findViewById(R.id.llHeader);//from the list_item_in_middle layout
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem > 0) { // 1st row will stick
llHeader.setVisibility(View.VISIBLE);
} else {
llHeader.setVisibility(View.GONE);
}
}
});
b) OnItemClickListener of listview
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int a = 0;
if(position == 0){//for clicking from date and to date.
TextView frm = (TextView) view.findViewById(R.id.frmdate);
frm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"From date clicked",Toast.LENGTH_SHORT).show();
}
});
TextView to = (TextView) view.findViewById(R.id.todate);
to.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"to date clicked",Toast.LENGTH_SHORT).show();
}
});
}else {
if(position == 1) {//for clicking the btn like list element.
swap();
Toast.makeText(MainActivity.this,"Header clicked",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
}
}
});
c) Linearlayout click function for element that gets stuck to the top
llHeader.setOnClickListener(new View.OnClickListener() { //for clicking the elements that gets stuck to the top
#Override
public void onClick(View v) {
swap();
Toast.makeText(MainActivity.this,"Header clicked",Toast.LENGTH_SHORT).show();
}
});
d) swap function called from linearlayout and the list element at position 1
public void swap(){
ltAdapter.notifyDataSetChanged();
new1 = new Integer[]{20, 21, 22, 23, 24, 25, 26, 27, 28};
ltAdapter.InitializeValues(new1);
}
NB:If any focusable view is taken in the listview, the OnItemclickListener doesnot work, read in stackoverflow itself
Finally the output:
before the second element click
after clicking
after scrolling
I think it happens in this way: when you scroll the page, linear layout gets invisible successfully but when you reache the button by doing scroll, the focus goes to listview and so it makes listview to scrolls instead of the whole page.
Do a fast scroll or scroll the page by swiping the button, not the listview and check if botton gets invisible.

ListView show only the first item in the list

I search a lot for an answer and didn't find one that suit me.
I found out that when I set my layout_heigh to 500dp -> all the item shown, but when it has normal size (layout_heigh=wrap_content) then only the first item is shown.
this is my code:
activity_main.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:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add Task"
android:onClick="addTask" />
<ListView android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
</LinearLayout>
xml_item.xml: this is for one item to duplicate in the listView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/itemText"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="15dp"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/doneBtn"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Done" />
</RelativeLayout>
MyListAdapter: a class in the main_activity that extends the ArrayAdapter class so I can put in one row both TextView and a button.
private class MyListAdapter extends ArrayAdapter<String> {
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout= resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainHolder= null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView= inflater.inflate(layout, parent, false);
ViewHolder viewHolder= new ViewHolder();
viewHolder.item= (TextView) convertView.findViewById(R.id.itemText);
viewHolder.btn= (Button) convertView.findViewById(R.id.doneBtn);
viewHolder.btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), itemArray.get(position), Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
}
else {
mainHolder= (ViewHolder) convertView.getTag();
mainHolder.item.setText(getItem(position));
}
return convertView;
}
public class ViewHolder {
TextView item;
Button btn;
}
}
Thanks for all! :)
but when it has normal size (layout_heigh=wrap_content)
It is a very bad idea using a ListView by giving it a height=wrap_content.
This forces ListView to measure a few children out of the adapter at layout time, to know how big it should be.
You can check it your self debugging the getView() method inside the adapter.
In your case, using a LinearLayout just add a top margin (50dp as the Button) to your listView and use layout_heigh=match_parent.
Otherwise use a RelativeLayout.
Change your listview to take up as much room as is can considering the height of any sibling views (in this case your button). Here's how to do that with the android:layout_weight attribute
<?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:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add Task"
android:onClick="addTask" />
<ListView android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>

Can't click on ListView whose rows are made of WebViews

I have an Activity which extends ListView
The ListView that I use is an extension of ArrayAdapter and each row of the ListView primarily consists of a WebView
The problem is that touching the WebView does not call OnListItemClick.
Any thoughts on how I can be able to touch the webview and call OnListItemClick?
Or is there something else that I'm doing wrong?
Thanks!
Here is a skeleton of the Activity class, along with the relevant XML files for the layout
PS, I've tried setting android:clickable="false" in my layout, but this doesn't let me click on the rows of my ListView.
Interestingly, I can still scroll through the list, but I just can't click!
public class ListOfItemsFromTopicActivity extends ListActivity {
private Topic t;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items_from_topic_skeleton);
TopicFactory tf = new TopicFactory();
t = tf.build_topic(3);
ListView itemlist = getListView();
ListOfItemsAdapter adapter = new ListOfItemsAdapter(this, R.layout.items_from_topic_row, t.getItems());
itemlist.setAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// do some stuff that never gets done
}
private class ListOfItemsAdapter extends ArrayAdapter<Item> {
private ArrayList<Item> items;
private int text_view_resource_id;
private LayoutInflater layout_inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public ListOfItemsAdapter(Context context, int a_text_view_resource_id, ArrayList<Item> some_items) {
super(context, a_text_view_resource_id, some_items);
this.items = some_items;
this.text_view_resource_id = a_text_view_resource_id;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = layout_inflater.inflate(this.text_view_resource_id, null);
Item o = items.get(position);
WebView page = (WebView)v.findViewById(R.id.item);
page.loadDataWithBaseURL("fake://a/bcde", o.getShort_title(), "text/html", "utf-8", "");
return v;
}
}
}
items_from_topic_skeleton.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
>
<TextView
android:background="#drawable/blackgrad"
android:gravity="center"
android:id="#+id/section_title"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:textColor="#ffffff"
android:textSize="18sp"
/>
<ListView
android:background="#ffffff"
android:cacheColorHint="#ffffffff"
android:id="#android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
items_from_topic_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="5dip"
>
<LinearLayout
android:gravity="center"
android:layout_height="60dip"
android:layout_width="fill_parent"
android:orientation="horizontal"
>
<LinearLayout
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:minHeight="55dip"
android:orientation="horizontal"
android:padding="5dip"
>
<WebView
android:id="#+id/item"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:scrollbars="none"
/>
</LinearLayout>
<ImageView
android:id="#+id/disclosure"
android:layout_height="29dip"
android:layout_width="29dip"
android:paddingRight="5dip"
android:src="#drawable/disclosure"
/>
</LinearLayout>
</LinearLayout>
If you add a focusable element to a ListView, it causes the ListView items to no longer be selectable. Try setting android:clickable = false on your WebView.
Just make your onTouch event to do whatever you want.
e.g: My list item consists of TextView, WebView and Linear layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="66dp"
android:orientation="vertical"
android:id="#+id/layout">
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView"
android:layout_height="wrap_content" />
<WebView
android:id="#+id/webvie"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</WebView>
</LinearLayout>
and I want to perform on click action - on the whole listitem.
holder.layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do stuff
}
});
But when I run my app, only texview is clickable. What to do? Set up onTouch listener on webView:
holder.webView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
holder.layout.callOnClick();
return true;
}
});
supports only API 15+ tough...

Categories

Resources