我目前正在开发一个类似于图库应用的应用。应用程序需要从资源文件夹加载一些图像,当用户选择一个图像时,应用程序应加载所选图像,如果用户滑动屏幕,应用程序应加载下一个图像或上一个图像。
我遇到的问题是,当我点击图片时,应用程序始终会加载第一张图片。我尝试使用int来解决它,但程序给了我一个错误。
我在android编程方面很新,对不起,如果这是一个简单的问题。
以下是该应用的代码:
主要活动:
package com.example.erasor.pruebaslide;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Image1 (View view) {
Intent intent= new Intent(this, Slide.class);
startActivity(intent);
}
public void Image2 (View view) {
Intent intent = new Intent(this, Slide.class);
intent.putExtra("position", 2);
startActivity(intent);
}
}
幻灯片活动:
package com.example.erasor.pruebaslide;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class Slide extends AppCompatActivity {
private CustomPagerAdapter mCustomPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slide);
Intent intent = getIntent();
int position = intent.getIntExtra("position", 0);
mCustomPagerAdapter = new CustomPagerAdapter(this);
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mCustomPagerAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
int[] mResources = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
};
class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.fragment_slide, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.image);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
}
}
答案 0 :(得分:0)
我认为你错过了告诉mViewPager要显示哪个位置的部分,试试这个:
www.mydomain.con/industries/accounting/<anything>