I implement HorizontalScrollView using this sample code and this . I have fetch data from server but i cant find way to implement onclick listener , How can I make horizontalscrollview click event ?
ArrayList<star> actorsList;
actorsList = new ArrayList<Actors>();
new JSONAsyncTask().execute("http://ift.tt/1tzZHvh");
centerLockHorizontalScrollview = (CenterLockHorizontalScrollview) findViewById(R.id.scrollView);
adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
centerLockHorizontalScrollview.setAdapter(MainActivity.this, adapter);
getView method :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
/// //// /////
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
//// //// /////
return v;
}
CenterLockHorizontalScrollview class
public class CenterLockHorizontalScrollview extends HorizontalScrollView {
Context context;
int prevIndex = 0;
public CenterLockHorizontalScrollview(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.setSmoothScrollingEnabled(true);
}
public void setAdapter(Context context,ActorAdapter mAdapter) {
try {
fillViewWithAdapter(mAdapter);
} catch (ZeroChildException e) {
e.printStackTrace();
}
}
private void fillViewWithAdapter(ActorAdapter mAdapter)
throws ZeroChildException {
if (getChildCount() == 0) {
throw new ZeroChildException(
"CenterLockHorizontalScrollView must have one child");
}
if (getChildCount() == 0 || mAdapter == null)
return;
ViewGroup parent = (ViewGroup) getChildAt(0);
parent.removeAllViews();
for (int i = 0; i < mAdapter.getCount(); i++) {
parent.addView(mAdapter.getView(i, null, parent));
}
}
public void setCenter(int index) {
ViewGroup parent = (ViewGroup) getChildAt(0);
View preView = parent.getChildAt(prevIndex);
preView.setBackgroundColor(Color.parseColor("#64CBD8"));
android.widget.LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(5, 5, 5, 5);
preView.setLayoutParams(lp);
View view = parent.getChildAt(index);
view.setBackgroundColor(Color.RED);
int screenWidth = ((Activity) context).getWindowManager()
.getDefaultDisplay().getWidth();
int scrollX = (view.getLeft() - (screenWidth / 2))
+ (view.getWidth() / 2);
this.smoothScrollTo(scrollX, 0);
prevIndex = index;
}
}
Aucun commentaire:
Enregistrer un commentaire