android 如何将图像从 horizontalscrollview 设置到 viewpager
how to set images into the viewpager from horizontalscrollview in android
我正在尝试将图像从 horizontalscrollview 设置到 viewpager。
我无法获取要在 setcurrentItem 方法中使用的 horizontalscrollview 的 ID。
我试了很多东西,但我做不到。
这是 MainActivity class
public class MainActivity extends AppCompatActivity {
int toplamSayfa = 5;
private View btnNext, btnPrev;
HorizontalScrollView hs;
ImageView imageView = null;
private Integer images[] = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e};
ViewPager viewPager;
LinearLayout myGallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomPagerAdapter adapter = new CustomPagerAdapter(this, toplamSayfa);
myGallery = (LinearLayout) findViewById(R.id.container);
hs = (HorizontalScrollView) findViewById(R.id.scrollview);
viewPager = (ViewPager) findViewById(R.id.view_pager);
btnNext = findViewById(R.id.next);
btnPrev = findViewById(R.id.prev);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
btnPrev.setOnClickListener(onClickListener(0));
btnNext.setOnClickListener(onClickListener(1));
addImagesToThegallery();
}
private void addImagesToThegallery() {
final LinearLayout imageGallery = (LinearLayout) findViewById(R.id.container);
for ( Integer image : images) {
imageGallery.addView(getImageView(image));
}
}
private View getImageView(final Integer image) {
imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//the method where i am stuck
viewPager.setCurrentItem();
}
});
imageView.setImageResource(image);
return imageView;
}
private View.OnClickListener onClickListener(final int i) {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (i > 0) {
if (viewPager.getCurrentItem() < viewPager.getAdapter().getCount() - 1) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
} else {
if (viewPager.getCurrentItem() > 0) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
}
}
};
}
}
试试这个,但忽略 if-else 部分:
package com.example.hotel_app_regularuser;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class PageTwo extends Activity {
ViewPager vpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_two);
vpager=(ViewPager)findViewById(R.id.viewPager);
PagerAdapter adapter = new customAdapter(PageTwo.this);
vpager.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.page_two, menu);
return true;
}
}
class customAdapter extends PagerAdapter
{
Context context;
int imageId[]={
R.drawable.chinese,
R.drawable.euro,
R.drawable.americanfood,
R.drawable.continental
};
public customAdapter(Context context)
{
this.context=context;
}
@Override
public Object instantiateItem(ViewGroup container,int position)
{
Typeface type = Typeface.createFromAsset(this.context.getAssets(), "fonts/Confetti.ttf");
LayoutInflater inflater=((Activity)context).getLayoutInflater();
View viewitem=inflater.inflate(R.layout.activity_page_two,container,false);
ImageView im=(ImageView)viewitem.findViewById(R.id.imageView1);
im.setImageResource(imageId[position]);
TextView textView1 = (TextView) viewitem.findViewById(R.id.textView1);
if(position==0)
{
textView1.setText("Chinese");
}
if(position==1)
{
textView1.setText("European");
}
if(position==2)
{
textView1.setText("American");
}
if(position==3)
{
textView1.setText("Continental");
}
textView1.setTypeface(type);
textView1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 70);
textView1.setTextColor(Color.RED);
((ViewPager)container).addView(viewitem);
return viewitem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageId.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==((View)arg1);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
}
要创建滚动视图,试试这个 xml 文件:
<!--
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Check_Availability"
android:stretchColumns="0,1"
android:id="@+id/main_table"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</TableLayout>
-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:id="@+id/main_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:id="@+id/header"/>
</TableLayout>
</ScrollView>
请注意,注释掉了使用 table 布局的页面的部分 ID。希望这可以帮助!
更新 addImagesToThegallery
方法以在将图像从 images
数组添加到图库时保留图像索引:
private void addImagesToThegallery() {
final LinearLayout imageGallery = (LinearLayout) findViewById(R.id.container);
int i = 0;
for ( Integer image : images) {
imageGallery.addView(getImageView(image,i));
i += 1;
}
}
并在此处使用 index
:
private View getImageView(final Integer image, int index) {
imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//use the index here
viewPager.setCurrentItem(index);
}
});
imageView.setImageResource(image);
return imageView;
}
我正在尝试将图像从 horizontalscrollview 设置到 viewpager。
我无法获取要在 setcurrentItem 方法中使用的 horizontalscrollview 的 ID。
我试了很多东西,但我做不到。
这是 MainActivity class
public class MainActivity extends AppCompatActivity {
int toplamSayfa = 5;
private View btnNext, btnPrev;
HorizontalScrollView hs;
ImageView imageView = null;
private Integer images[] = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e};
ViewPager viewPager;
LinearLayout myGallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomPagerAdapter adapter = new CustomPagerAdapter(this, toplamSayfa);
myGallery = (LinearLayout) findViewById(R.id.container);
hs = (HorizontalScrollView) findViewById(R.id.scrollview);
viewPager = (ViewPager) findViewById(R.id.view_pager);
btnNext = findViewById(R.id.next);
btnPrev = findViewById(R.id.prev);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
btnPrev.setOnClickListener(onClickListener(0));
btnNext.setOnClickListener(onClickListener(1));
addImagesToThegallery();
}
private void addImagesToThegallery() {
final LinearLayout imageGallery = (LinearLayout) findViewById(R.id.container);
for ( Integer image : images) {
imageGallery.addView(getImageView(image));
}
}
private View getImageView(final Integer image) {
imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//the method where i am stuck
viewPager.setCurrentItem();
}
});
imageView.setImageResource(image);
return imageView;
}
private View.OnClickListener onClickListener(final int i) {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (i > 0) {
if (viewPager.getCurrentItem() < viewPager.getAdapter().getCount() - 1) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
}
} else {
if (viewPager.getCurrentItem() > 0) {
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
}
}
};
}
}
试试这个,但忽略 if-else 部分:
package com.example.hotel_app_regularuser;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class PageTwo extends Activity {
ViewPager vpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_two);
vpager=(ViewPager)findViewById(R.id.viewPager);
PagerAdapter adapter = new customAdapter(PageTwo.this);
vpager.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.page_two, menu);
return true;
}
}
class customAdapter extends PagerAdapter
{
Context context;
int imageId[]={
R.drawable.chinese,
R.drawable.euro,
R.drawable.americanfood,
R.drawable.continental
};
public customAdapter(Context context)
{
this.context=context;
}
@Override
public Object instantiateItem(ViewGroup container,int position)
{
Typeface type = Typeface.createFromAsset(this.context.getAssets(), "fonts/Confetti.ttf");
LayoutInflater inflater=((Activity)context).getLayoutInflater();
View viewitem=inflater.inflate(R.layout.activity_page_two,container,false);
ImageView im=(ImageView)viewitem.findViewById(R.id.imageView1);
im.setImageResource(imageId[position]);
TextView textView1 = (TextView) viewitem.findViewById(R.id.textView1);
if(position==0)
{
textView1.setText("Chinese");
}
if(position==1)
{
textView1.setText("European");
}
if(position==2)
{
textView1.setText("American");
}
if(position==3)
{
textView1.setText("Continental");
}
textView1.setTypeface(type);
textView1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 70);
textView1.setTextColor(Color.RED);
((ViewPager)container).addView(viewitem);
return viewitem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageId.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==((View)arg1);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
}
要创建滚动视图,试试这个 xml 文件:
<!--
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Check_Availability"
android:stretchColumns="0,1"
android:id="@+id/main_table"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</TableLayout>
-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:id="@+id/main_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:id="@+id/header"/>
</TableLayout>
</ScrollView>
请注意,注释掉了使用 table 布局的页面的部分 ID。希望这可以帮助!
更新 addImagesToThegallery
方法以在将图像从 images
数组添加到图库时保留图像索引:
private void addImagesToThegallery() {
final LinearLayout imageGallery = (LinearLayout) findViewById(R.id.container);
int i = 0;
for ( Integer image : images) {
imageGallery.addView(getImageView(image,i));
i += 1;
}
}
并在此处使用 index
:
private View getImageView(final Integer image, int index) {
imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 10, 0);
imageView.setLayoutParams(lp);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//use the index here
viewPager.setCurrentItem(index);
}
});
imageView.setImageResource(image);
return imageView;
}