Android 水平显示项目?
我只想显示RSS提要中的一些图像数据。我的RSS源代码运行良好,图像也可以从源正确加载,但我无法以水平方式显示图像?我试过使用Gallery,但无法将图像放入其中?有没有办法达到同样的效果 我还引用了实现水平ListView的链接,但是我无法实现列表的onClickListener。请帮忙。多谢各位 我为gallery尝试的代码 公共类ImageAdapter扩展了BaseAdapter {Android 水平显示项目?,android,listview,android-gallery,horizontallist,Android,Listview,Android Gallery,Horizontallist,我只想显示RSS提要中的一些图像数据。我的RSS源代码运行良好,图像也可以从源正确加载,但我无法以水平方式显示图像?我试过使用Gallery,但无法将图像放入其中?有没有办法达到同样的效果 我还引用了实现水平ListView的链接,但是我无法实现列表的onClickListener。请帮忙。多谢各位 我为gallery尝试的代码 公共类ImageAdapter扩展了BaseAdapter { int图像背景; 私有列表对象=null; 私人语境; 公共图像适配器(上下文c) { 上下文=c; T
int图像背景;
私有列表对象=null;
私人语境;
公共图像适配器(上下文c)
{
上下文=c;
TypedArray ta=获得的StyledAttributes(R.styleable.Gallery1);
imageBackground=ta.getResourceId(R.styleable.Gallery1\u android\u galleryItemBackground,1);
ta.recycle();
}
公共ImageAdapter(上下文上下文、int textViewResourceId、列表对象)
{
超级();
this.context=上下文;
this.objects=对象;
}
@凌驾
public int getCount()
{
返回此.objects.size();
//返回图片长度;
}
@凌驾
公共对象getItem(int位置)
{
返回此.objects.get(位置);
//返回位置;
}
@凌驾
公共长getItemId(int位置)
{
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
RSSIteam数据=(RSSIteam)getItem(位置);
字符串imageUrl=data.imageUrl;
尝试
{
URL feedImage=新URL(imageUrl);
HttpURLConnection conn=(HttpURLConnection)feedImage.openConnection();
InputStream is=conn.getInputStream();
位图img=BitmapFactory.decodeStream(is);
}
捕获(格式错误)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
ImageView iv=新的ImageView(上下文);
iv.setImageResource(img[position]);//这里我无法将图像设置在特定位置,因为它需要int类型数组
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.设置布局参数(新画廊布局参数(90,70));
iv.挫折背景资源(图像背景);
回报四;
}
}您可以使用galleryholder.xml。此xml包含imageview,它由适配器和返回视图充气,适配器在库中填充此视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:background="@drawable/my_border"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/storyImage"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:scaleType="fitXY" />
</LinearLayout>
and use Image adapter to fill the gallery
galleryID.setAdapter(new ImageAdapter());
Image Adapter code
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater layoutInflater=(LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=(View)layoutInflater.inflate(R.layout.galleryholder,parent,false);
ImageView iv;
if(convertView==null){
convertView=view;
iv=(ImageView)view.findViewById(R.id.storyImage);
}
RSSIteam data = (RSSIteam) getItem(position);
String imageUrl = data.imageurl;
try
{
URL feedImage = new URL(imageUrl);
HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection();
InputStream is = conn.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
iv.setImageResource(img[position]); //here i am unable to set the image in particular position because it require int type array
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(90,70));
iv.setBackgroundResource(imageBackground);
return convertView;
}
并使用图像适配器填充库
setAdapter(新的ImageAdapter());
图像适配器代码
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
LayoutInflater LayoutInflater=(LayoutInflater)
getSystemService(context.LAYOUT\u充气机\u服务);
视图=(视图)布局平坦。充气(R.layout.galleryholder,父级,false);
ImageView iv;
if(convertView==null){
转换视图=视图;
iv=(ImageView)view.findViewById(R.id.storyImage);
}
RSSIteam数据=(RSSIteam)getItem(位置);
字符串imageUrl=data.imageUrl;
尝试
{
URL feedImage=新URL(imageUrl);
HttpURLConnection conn=(HttpURLConnection)feedImage.openConnection();
InputStream is=conn.getInputStream();
位图img=BitmapFactory.decodeStream(is);
}
捕获(格式错误)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
iv.setImageResource(img[position]);//这里我无法将图像设置在特定位置,因为它需要int类型数组
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.设置布局参数(新画廊布局参数(90,70));
iv.挫折背景资源(图像背景);
返回视图;
}
查看下面的代码,这些代码将对您完全有帮助。它工作正常
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
* // here i am setting the text color and bckgroung of layout //
* According to time if (6 <= (new Date()).getHours() && (new
* Date()).getHours() <= 17) { ll.setBackgroundColor(Color.BLACK);
* label.setTextColor(Color.WHITE); } else {
* ll.setBackgroundColor(Color.WHITE);
* label.setTextColor(Color.BLACK); }
*/
// return (row);
View v = convertView;
if (v == null) {
LayoutInflater inflater = context.getLayoutInflater();
v = inflater.inflate(R.layout.myrow, null);
}
Item o = itemsCmn.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.label);
ImageView icon = (ImageView) v.findViewById(R.id.icon);
if (tt != null) {
tt.setText(o.getTitle());
}
else{
}
if (icon != null) {
if (o.getImg() != null) {
icon.setImageBitmap(BitmapFactory.decodeByteArray(
o.getImg(), 0, o.getImg().length));
}else{
icon.setImageResource(R.drawable.images);
}
}
}
return v;
}// getView
@覆盖
公共视图getView(int位置、视图转换视图、视图组父视图){
/*
*//这里我正在设置布局的文本颜色和bckgroung//
*根据时间,如果(6)“我尝试了画廊,但无法将图像放入其中…”-发布您尝试过的代码。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
* // here i am setting the text color and bckgroung of layout //
* According to time if (6 <= (new Date()).getHours() && (new
* Date()).getHours() <= 17) { ll.setBackgroundColor(Color.BLACK);
* label.setTextColor(Color.WHITE); } else {
* ll.setBackgroundColor(Color.WHITE);
* label.setTextColor(Color.BLACK); }
*/
// return (row);
View v = convertView;
if (v == null) {
LayoutInflater inflater = context.getLayoutInflater();
v = inflater.inflate(R.layout.myrow, null);
}
Item o = itemsCmn.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.label);
ImageView icon = (ImageView) v.findViewById(R.id.icon);
if (tt != null) {
tt.setText(o.getTitle());
}
else{
}
if (icon != null) {
if (o.getImg() != null) {
icon.setImageBitmap(BitmapFactory.decodeByteArray(
o.getImg(), 0, o.getImg().length));
}else{
icon.setImageResource(R.drawable.images);
}
}
}
return v;
}// getView