我有一个java代码作为HomeBaseAdapter.java作为一个附加功能,我必须启用onclick监听器以及java类的其他功能,如图像滑块等:
此处案例:1正在编码文章布局。在这里,我希望图像滑块与图像的onclick监听器一起显示
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import static android.content.Context.MODE_PRIVATE;
class HomeBaseAdapter {
private AppCompatActivity activity;
private LayoutInflater inflater = null;
private JSONObject jsonObject;
private String[] heading = new String[]{"Business Directory", "Articles", "Gallery", "Institutes", "Bus Routes", "Property Listing", "Traffic Live News", "Events"};
private Set<String> firstSet;
private ArrayList<String> list = new ArrayList<>();
HomeBaseAdapter(AppCompatActivity a) throws JSONException {
activity = a;
jsonObject = new JSONObject(PreferenceManager.getDefaultSharedPreferences(activity).getString("theJson", ""));
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView listView = (ListView)a.findViewById(R.id.listView1);
listView.setAdapter(new BaseAdapter2());
SharedPreferences listpref = a.getSharedPreferences("list", MODE_PRIVATE);
Set<String> set = listpref.getStringSet("DATE_LIST", null);
list.addAll(set);
firstSet = new HashSet<>(list);
Set<String> secondSet = new HashSet<>(Arrays.asList(heading));
firstSet.retainAll(secondSet);
}
private class ViewHolder {
TextView heading;
TextView bannerView;
ImageView img;
Button more;
LinearLayout linearLayout;
}
public class BaseAdapter2 extends BaseAdapter {
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@SuppressLint({"InflateParams", "SetTextI18n"})
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.listview_home_items, null);
holder.img =(ImageView) convertView.findViewById(R.id.imageView);
holder.heading =(TextView) convertView.findViewById(R.id.heading);
holder.more =(Button) convertView.findViewById(R.id.more);
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.l1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.heading.setText(list.get(position));
holder.more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (list.get(position) == "Articles") {
activity.startActivity(new Intent(activity, Article.class));
} else if (list.get(position) == "Business Directory") {
activity.startActivity(new Intent(activity, BussinessDirectory.class));
} else if (list.get(position) == "Gallery") {
activity.startActivity(new Intent(activity, Gallery1.class));
}
}
});
holder.linearLayout.addView(rowItems(position));
Toast.makeText(activity, ""+position, Toast.LENGTH_SHORT).show();
Log.i("position",""+position);
return convertView;
}
}
View rowItems(int position) {
try {
String json = jsonObject.getString(list.get(position));
JSONObject jObject = new JSONObject(json);
Iterator<String> keys = jObject.keys();
while (keys.hasNext()) {
String key = keys.next();
if (!key.equals("")) {
JSONObject innerJObject = jObject.getJSONObject(key);
switch (Arrays.asList(heading).indexOf(list.get(position))) {
case 0: {
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.row_home, null);
TextView title = (TextView) view.findViewById(R.id.textViewTitle);
TextView address1 = (TextView)view.findViewById(R.id.textViewAddress1);
TextView area =(TextView) view.findViewById(R.id.textViewArea);
TextView pincode = (TextView)view.findViewById(R.id.textViewPinCode);
TextView phno =(TextView) view.findViewById(R.id.textViewPhoneNumber);
title.setText(innerJObject.getString("ShopTitle"));
address1.setText(innerJObject.getString("Address1"));
area.setText(innerJObject.getString("Area"));
pincode.setText("Chennai-" + innerJObject.getString("PinCode"));
phno.setText(innerJObject.getString("PhoneNumber"));
return view;
}
case 1: {
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.row_home_image, null);
TextView title = (TextView)view.findViewById(R.id.textViewTitle);
TextView desc = (TextView)view.findViewById(R.id.textViewDescription);
ImageView img = (ImageView) view.findViewById(R.id.imageView);
Picasso.with(activity).load("http://www.northmadras.com/upload/Articles/" + innerJObject.getString("Image")).resize(400, 400).into(img);
title.setText(innerJObject.getString("Title"));
desc.setText(innerJObject.getString("Prelode"));
return view;
}
}
}
}
} catch (Exception e) {
}
return null;
}
}
以下是我用于在图像滑块中显示图像的两个类
使用自定义类的article.java是MyCustomPageradapter.java
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class Article extends AppCompatActivity {
ViewPager viewPager;
static SimpleAdapter adapter;
static int startIndex;
ArrayList<ArrayList<String>> arrayList;
//int images[] = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3, R.drawable.image_4};
MyCustomPagerAdapter myCustomPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article);
viewPager = (ViewPager) findViewById(R.id.viewPager);
arrayList=new ArrayList<>();
new Article.json(startIndex).execute();
startIndex+=10;
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Toast.makeText(Article.this, ""+position+"--"+positionOffset+"--"+positionOffsetPixels, Toast.LENGTH_SHORT).show();
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@SuppressLint("StaticFieldLeak")
class json extends AsyncTask<Void,Void,ArrayList<ArrayList<String>>>{
ProgressDialog pDialog;
int startIndex;
public json(int startIndex) {
this.startIndex=startIndex;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Article.this);
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Logging in ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected ArrayList<ArrayList<String>> doInBackground(Void... voids) {
UserFunctions userFunction = new UserFunctions();
arrayList.addAll(userFunction.getArticle(startIndex));
try
{
Log.e("aaa",arrayList.toString());
} catch (Exception ex) {
}
return arrayList;
}
@Override
protected void onPostExecute(ArrayList<ArrayList<String>> arrayList) {
try {
//adapter.notifyDataSetChanged();
myCustomPagerAdapter = new MyCustomPagerAdapter(Article.this, arrayList);
viewPager.setAdapter(myCustomPagerAdapter);
} catch (Exception e) {Log.e("err",e+"error 1234567890");
} pDialog.dismiss();
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
}
}
}
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class MyCustomPagerAdapter extends PagerAdapter{
Context context;
ArrayList<ArrayList<String>> images;
LayoutInflater layoutInflater;
public MyCustomPagerAdapter(Context context, ArrayList<ArrayList<String>> images) {
this.context = context;
this.images = images;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
View itemView = layoutInflater.inflate(R.layout.item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
TextView textView = (TextView) itemView.findViewById(R.id.textView2);
//imageView.setImageResource(images[position]);
Picasso.with(context).load("http://www.northmadras.com/upload/Articles/"+images.get(position).get(0)).into(imageView);
textView.setText(images.get(position).get(1));
Log.e("img","http://www.northmadras.com/upload/Articles/"+images.get(position).get(1));
container.addView(itemView);
//listening to image click
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(context,ArticleContent.class));
}
});
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
答案 0 :(得分:0)
您可以创建界面CustomItemClickListener
public interface CustomItemClickListener {
public void onItemClick(View v, int position, int id);
public void onItemLongClick(View v, int position, int id);
}
然后在viewPagerAdapter
contructor
CustomItemClickListener listener;
...........
public MyCustomPagerAdapter(Context context, ArrayList<ArrayList<String>> images , CustomItemClickListener listener) {
this.context = context;
this.images = images;
this.listener = listener;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
然后用作
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onItemClick(v, myViewHolder.getAdapterPosition(), marqueesLists.get(myViewHolder.getAdapterPosition()).getMarqueeId());
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
listener.onItemLongClick(v , myViewHolder.getAdapterPosition() ,marqueesLists.get(myViewHolder.getAdapterPosition()).getMarqueeId());
return true;
}
});
然后在Activity
myCustomPagerAdapter = new MyCustomPagerAdapter(Article.this, arrayList,new CustomItemClickListener() {
@Override
public void onItemClick(View v, int position , int id) {
//do your `onClick` work here
}
@Override
public void onItemLongClick(View v, int position , int id) {
}
});
希望这有帮助。
根据您的要求更改值
答案 1 :(得分:-1)
使用View.onClickListener
实施您的课程并覆盖方法onClick
。
在那个(覆盖)onClick
方法中,您可以处理点击事件。