gpt4 book ai didi

java - Android创建缩略图布局

转载 作者:行者123 更新时间:2023-11-30 09:01:15 24 4
gpt4 key购买 nike

我在对话框中扩充布局,此布局显示电子书的许多缩略图 (96 ImageViews)。当我点击某个 ImageView 时,它会将我带到某个页面。关键是我有 96 个“if”语句,所以我想让代码更简单。我正在寻求建议或帮助以实现这一目标。这是我的代码:

缩略图布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#b8c9bc"
android:weightSum="1"
android:orientation="horizontal" >

<LinearLayout
android:id="@+id/thumblayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1"
android:layout_weight="0.2"
android:gravity="left"
android:visibility="visible" >

<ScrollView
android:id="@+id/ScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.9" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@drawable/thumbpageh1" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/thumbpageh2" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/thumbpageh3" />

<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/thumbpageh4" />

<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/thumbpageh5" />

.
.
.
.
.
.
.
.


<ImageView
android:id="@+id/imageView101"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="@drawable/thumbpageh97" />
</LinearLayout>

</ScrollView>


<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/but"
android:text="OK"
android:textColor="#e2edd3"
android:textSize="18sp"
android:layout_weight="0.1"
android:textStyle="bold" />


</LinearLayout>







</LinearLayout>

Java 代码:

       thumbbutton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {


final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.thumblayout);


WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 10; //x position
wmlp.y = 10; //y position



dialog.show();


final Button cancel=(Button)dialog.findViewById(R.id.button1);
final ImageView thumbpage1=(ImageView)dialog.findViewById(R.id.imageView1);
final ImageView thumbpage2=(ImageView)dialog.findViewById(R.id.imageView2);
final ImageView thumbpage3=(ImageView)dialog.findViewById(R.id.imageView3);
.
.
.
.
.
.
.
final ImageView thumbpage96=(ImageView)dialog.findViewById(R.id.imageView100);
final ImageView thumbpage97=(ImageView)dialog.findViewById(R.id.imageView101);

cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.hide();
// x=hsv.getScrollX();
// y=hsv.getScrollY();
// Toast.makeText(getBaseContext(), "X="+String.valueOf(x)+" and Y="+String.valueOf(y), Toast.LENGTH_LONG).show();
}
});

thumbpage1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page1);
pagenumber=1;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText(String.valueOf(p1)+" - "+String.valueOf(p2));

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});


thumbpage2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page2);
pagenumber=2;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText(String.valueOf(p1)+" - "+String.valueOf(p2));

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});

thumbpage3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page3);
pagenumber=3;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText(String.valueOf(p1)+" - "+String.valueOf(p2));

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});

thumbpage4.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page4);
pagenumber=4;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText(String.valueOf(p1)+" - "+String.valueOf(p2));

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});

thumbpage5.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page5);
pagenumber=5;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText(String.valueOf(p1)+" - "+String.valueOf(p2));

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});

thumbpage6.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page6);
pagenumber=6;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText(String.valueOf(p1)+" - "+String.valueOf(p2));

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});

thumbpage7.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page7);
pagenumber=7;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText(String.valueOf(p1)+" - "+String.valueOf(p2));

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});

thumbpage8.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page8);
pagenumber=8;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText(String.valueOf(p1)+" - "+String.valueOf(p2));

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});

thumbpage9.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page9);
pagenumber=9;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText(String.valueOf(p1)+" - "+String.valueOf(p2));

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});

.
.
.
.
.
.
.
.
.




thumbpage96.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page96);
pagenumber=96;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText(String.valueOf(p1)+" - "+String.valueOf(p2));

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});
thumbpage97.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
view1.setImageResource(R.drawable.page97);
pagenumber=97;
int p1=(pagenumber*2)-2;
int p2=(pagenumber*2)-1;
pages.setText("192");

myDbHelper.openDataBase();
checkMemoButtonStatus();
myDbHelper.close();
}
});

// visible++;
//
// if(visible==1)
// {
// thumblayout.setVisibility(View.VISIBLE);
// thumblayout.setVisibility(0);
// }
// else
// {
// thumblayout.setVisibility(View.GONE);
// visible=0;
//
// }





// TODO Auto-generated method stub
}
});

我搜索过关于使用带有图像的自定义 ListView ,我也搜索过关于在 ScrollView 中获取 View 的位置,但没有提供太多帮助,谢谢

最佳答案

所以我不会向您提供所有代码,但我会尝试引导您如何使用 ListView 来解决您的问题。

所以首先你需要将整个 ScrollView 替换为 ListView,然后为带有自定义 View 的 ListView 创建一个 ArrayAdapter

这是一个 ArrayAdapter 的例子:

public class MyAdapter extends ArrayAdapter<String>{

private static ViewHolder viewHolder;
private final Context context;
private ArrayList<String> urls;

public NewsAdapter(Context context, ArrayList<String> urls) {
super(context, R.layout.fragment_newsfeed, urls);
this.context = context;
this.urls = urls;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_child, parent, false);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.imageItem.setImageDrawable(null);
}
final String url = urls.get(position);
//SETUP THE IMAGE HERE
return convertView;
}

@Override
public int getCount() {
return urls.size();
}

static class ViewHolder {
ImageView imageView;
}

然后您可以传递 URL 或 Drawable 名称或您拥有的任何图像源。如果您使用 URL,我建议您使用 Picasso imageloader 库很容易使用。

如果您对我的回答有任何其他问题,请随时在评论中提问 :)

关于java - Android创建缩略图布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26462159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com