I have a list that implementing both of PullToRefresh (the old one) and SwipeListView library.
Im following this to combining those library and this to use it in the Activity.
My list can do some basic functionalities like pullUp/pullDown and swipe left/right but i encountered some bug :
The item position in my listview is always started from 1, i believe it should be started from 0, so i need to decrease it in my methods.
When i swipe an item (say, the first item), the item 5th item will be swiped to. So the index+4 item will also be swiped.
The code i used to initialize the objects :
private PullToRefreshSwipeListView ptrListView;
private SwipeListView resultListView;
resultListView = ptrListView.getRefreshableView();
ptrListView.setOnRefreshListener(new OnRefreshListener2<SwipeListView>() {
#Override
public void onPullDownToRefresh(PullToRefreshBase<SwipeListView> refreshView) {
// TODO Auto-generated method stub
}
#Override
public void onPullUpToRefresh(PullToRefreshBase<SwipeListView> refreshView) {
// TODO Auto-generated method stub
}
});
This is the method i used to initialize the listview :
private void setListview() {
adapter = new LibraryAdapter(this, R.layout.item_library_list, new ArrayList<PurchasedItem>(), resultListView);
adapter.setListener(new LibraryListListener() {
//set the adapter
});
resultListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
//position di -1 karena sejak gabung library swipelistview + pulltorefresh, position slalu kelebihan 1 & menyebabkan OutOfBound error.
#Override
public void onClickFrontView(final int position) {
//do something here
}
#Override
public void onOpened(int position, boolean toRight) {
// TODO Auto-generated method stub
super.onOpened(position-1, toRight);
lastPos = position-1;
}
#Override
public void onMove(int position, float x) {
// TODO Auto-generated method stub
super.onMove(position-1, x);
}
#Override
public int onChangeSwipeMode(int position) {
// TODO Auto-generated method stub
return SwipeListView.SWIPE_MODE_DEFAULT;
}
#Override
public void onStartOpen(int position, int action, boolean right) {
// TODO Auto-generated method stub
super.onStartOpen(position-1, action, right);
}
});
resultListView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapter, View arg1,
final int pos, long arg3) {
//do something here
}
});
ptrListView.setAdapter(adapter);
ptrListView.setLongClickable(true);
resultListView.setSwipeOpenOnLongPress(false);
}
And this is my xml :
<com.handmark.pulltorefresh.library.PullToRefreshSwipeListView
xmlns:swipe="http://schemas.android.com/apk/res-auto"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#drawable/line_separator_repeat"
android:listSelector="#00000000"
ptr:ptrMode="pullFromEnd"
swipe:swipeActionLeft="reveal"
swipe:swipeBackView="#+id/back"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeFrontView="#+id/front"
swipe:swipeMode="both" />
Please kindly help me, any help is appreciated. Thanks
I had the same first issue like you. i think the first item (index 0) is the header.
int _index = index - listView.getHeaderViewsCount();
I had never met your second problem. You can try this to solve touching problems :
listView.setOnScrollListener(listView.getRefreshableView().getTouchListener().makeScrollListener());
Hope this help
In order to fix problem no. 2, just add those both Override functions to your adapter:
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 500;
}
Related
In Oncreate of my MainActivity I add this code:
mDateAreaSlider.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// Hide right arrow if reach last position
if (arg0 == adapter.DateList.length - 1) {
mButtonNext.setVisibility(View.INVISIBLE);
mButtonBack.setVisibility(View.VISIBLE);
}
// Hide left arrow if reach first position
else if (arg0 == 0) {
mButtonBack.setVisibility(View.INVISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
// Else show both arrows
else {
mButtonBack.setVisibility(View.VISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
ifSingding = true;
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
and on the button click I add this code:
mDateAreaSlider.setCurrentItem(0);
but the problem is it can slide to the page I selected but does not make my arrows invisible.
NOTE: The 2 arrows are ImageViews in main layout,not inflated layout.So it is nothing to do with adapter.
Replace your original code with this:
final ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == adapter.DateList.length - 1) {
mButtonNext.setVisibility(View.INVISIBLE);
mButtonBack.setVisibility(View.VISIBLE);
}
// Hide left arrow if reach first position
else if (position == 0) {
mButtonBack.setVisibility(View.INVISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
// Else show both arrows
else {
mButtonBack.setVisibility(View.VISIBLE);
mButtonNext.setVisibility(View.VISIBLE);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
mDateAreaSlider.addOnPageChangeListener(onPageChangeListener);
Execute this code on your button click
mDateAreaSlider.setCurrentItem(0);
mDateAreaSlider.post(new Runnable() {
#Override
public void run() {
onPageChangeListener.onPageSelected(0);
}
});
I have a function of showDiaglog.
I just want to, when user tap checkbox the diaglog shown, and the spinner also, but I got a problem with listener. Everytime I open settings, the popup show up twice.
First because of listener of spinner then second because one of my setting checked.
I save every changes in that setting.
Can you help me?
selectLanguage
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
saveSettingan(Key_Select, position);
if (position == 0) {
setDefaultLocal();
} else {
setLocal("in");
}
showDialog();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
and this one
case R.id.checkAcak:
saveSettingan(Key_Acak, isChecked);
showDialog();
break;
You could store a boolean which remembers the visibility of the dialog. Your show dialog should include this:
if (isDialogVisible)
return;
isDialogVisible = true;
Then you set a dismiss listener to set isDialogVisible to false.
In my application i have used an "Listview". In that what i want is I have to get the count of the visible child.For example if i load that listview in my device first shoes 3 rows ,so i have to show in a textview that "Showing 3 of 10",and when user scroll the listview i have to update that textview according to scroll..I tried a lot but i didn't acheived this..
I tried for this also:
int count=0;
for(int i = 0; i <= listView.getLastVisiblePosition(); i++)
{
if(listView.getChildAt(i)!= null)
{
count++; // saying that view that counts is the one that is not null, because sometimes you have partially visible items....
}
}
Always returning 0. Please help me to solve this.
Y i got the answer....
I implemented onscroll listener..
library_listview.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
firstVisibleItem = firstVisibleItem+1;
String vischild="Showing "+firstVisibleItem+" to "+visibleItemCount+" of "+totalItemCount+"";
visiblechild.setText(vischild);
}
});
Thanks a lot..
i am using java android .
i have a problem in my app and i need some code that can make my item has two lines of texts and one image .
please save me and help .
here is the code for what i tried please make sure that you can help and give your opinion and how i can solve it cause i stucked here.
the main activity
private void setUpView() {
// TODO Auto-generated method stub
etInput = (EditText)this.findViewById(R.id.editText_input);
Pinput = (EditText)this.findViewById(R.id.editText1);
btnAdd = (Button)this.findViewById(R.id.button_add);
lvItem = (ListView)this.findViewById(R.id.listView_items);
itemArrey = new ArrayList<String>();
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
etInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER){
addItemList();
}
return false ;
}
});
Pinput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(keyCode== KeyEvent.KEYCODE_NUM) {
addItemList();
}
return;
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (isInputValid(Pinput )) {
itemArrey.add(Pinput.getText().toString());
itemArrey.add(etInput.getText().toString());
Pinput.setText("");
etInput.setText("");
itemAdapter.notifyDataSetChanged();
} if (isInputValid(etInput )) {
itemArrey.add(Pinput.getText().toString());
itemArrey.add(etInput.getText().toString());
Pinput.setText("");
etInput.setText("");
itemAdapter.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText etInput2 ) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
return false;
} else {
return true;
}
}
}
If you want a custom view in your ListView I recommend doing one xml layout by yourself and replace android.R.layout.simple_list_item_1 in your Adapter call with the new created one.
This would be the easiest way but as I think that you might need more freedom to create/modify the content of the row, I strongly suggest to create your own Adapter extending from BaseAdapter. In the getView() method of the adapter you can easily inflate and set up the layout like you want for that row.
Okie... you tag android-listview and list view item. So I think you need list view with image and and Text.
For that you have to use Custom List View.
See this
May be this will help you. and not then pls make your que more clear. :)
public class CompanySearchActivity extends RathbonesActivity {
private CompanySearchAdapter mStockListAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.companysearch_layout);
final EditText keywordET = (EditText)findViewById(R.id.codeET);
final Button search = (Button)findViewById(R.id.button_stock_add);
final Activity a= CompanySearchActivity.this;
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String keyword = keywordET.getText().toString();
Log.i("keyword: ",keyword);
ArrayList codearr = getResults(keyword);
mStockListAdapter = new CompanySearchAdapter(a,codearr);
ListView listview = (ListView) findViewById(R.id.stocklist);
listview.setAdapter(mStockListAdapter);
listview.setOnItemClickListener(this);
listview.setOnItemLongClickListener(this);
}
});
}
The lines listview.setOnItemClickListener(this);
listview.setOnItemLongClickListener(this); are giving errors because of this keyword, i replaced it with 'a' too but it doesnt work. What can be the possible way to achieve this?
If you wish to use the parent activities onClick method your activity must implement OnItemClickListener and OnItemLongClickListener
public class CompanySearchActivity extends RathbonesActivity implements OnItemClickListener, OnItemLongClickListener
{
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
return false;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
}
}
Note the code "implements OnItemClickListener, OnItemLongClickListener"
This is vital for implementing it this way.
Then you can call:
listview.setOnItemClickListener(CompanySearchActivity.this);
listview.setOnItemLongClickListener(CompanySearchActivity.this);
The this keyword is a reference to the object that owns the currently executing method. In this case this refers to the anonymous View.OnClickListener object that you are defining. Try replacing this with CompanySearchActivity.this
your listview onClick definition should look like your search listener.
search.setOnClickListener(new View.OnClickListener() {
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String keyword = keywordET.getText().toString();
Log.i("keyword: ",keyword);
ArrayList codearr = getResults(keyword);
mStockListAdapter = new CompanySearchAdapter(a,codearr);
}
});
ListView listview = (ListView) findViewById(R.id.stocklist);
listview.setAdapter(mStockListAdapter);
listview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
<do somthing when its clicked>
}
});
also ensure if you have multiple layouts that these view items (listview and search)
are in companysearch_layout.xml