大家好,又见面了,我是你们的朋友全栈君。
Java interface有多种用法,最常见的就是通过interface传递数据
- 第一种
public interface IData {
void value(String str);
}
public class ClassA {
public void sendData(IData idata){
idata.value("This is ClassA'value");
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassA ca = new ClassA();
ca.sendData(new IData() {
@Override
public void value(String str) {
// TODO Auto-generated method stub
System.out.println(str);
}
});
}
}
以上为最简单的数据传递用法
- 第二种
public interface IPositionListener {
void selectedPosition(int postion);
}
public class Adapter extends PagerAdapter{
private List<ImageView> viewList;
IPositionListener iPositionListener;
public Adapter(List<ImageView> viewList) {
this.viewList = viewList;
}
@Override
public int getCount() {
return viewList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 实例化
iPositionListener.selectedPosition(position);
container.addView(viewList.get(position));
return viewList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// 销毁
container.removeView(viewList.get(position));
}
public void setOnItemClickListener(IPositionListener iPositionListener) {
this.iPositionListener = iPositionListener;
}
}
private void initView() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
for (int i = 0; i< imgs.length; i++){
img = new ImageView(this);
img.setImageResource(imgs[i]);
viewList.add(img);
}
initViewPagerScroll();
Adapter adapter = new Adapter(viewList);
viewPager.setAdapter(adapter);
mHandler.sendEmptyMessage(0x11);
adapter.setOnItemClickListener(new IPositionListener() {
@Override
public void selectedPosition(int postion) {
Toast.makeText(MainActivity.this,"第"+(postion+1)+"张图片",Toast.LENGTH_LONG).show();
PicClick(postion);
}
});
}
private void PicClick(int postion) {
switch (postion){
// TODO
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/160352.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...