Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GridView是否在自定义适配器中仅显示单行?_Android_Gridview - Fatal编程技术网

Android GridView是否在自定义适配器中仅显示单行?

Android GridView是否在自定义适配器中仅显示单行?,android,gridview,Android,Gridview,我试图用自定义适配器动态添加gridview,但它只显示单行 我曾尝试过包装内容、匹配父对象、填充父对象的高度,但对我来说不起作用 如果我通过的是静态高度,比如1000,那么它显示了有什么解决方案吗 //栅格视图代码(mainLayout是我的父布局) //适配器编码 package customAdapters; import java.util.List; import PropertiesClasses.PlayerProperties; import android.app.Ac

我试图用自定义适配器动态添加gridview,但它只显示单行

我曾尝试过包装内容、匹配父对象、填充父对象的高度,但对我来说不起作用

如果我通过的是静态高度,比如1000,那么它显示了有什么解决方案吗

//栅格视图代码(mainLayout是我的父布局)

//适配器编码

package customAdapters;

import java.util.List;



import PropertiesClasses.PlayerProperties;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PlayerAdapter extends BaseAdapter {


    private List<PlayerProperties> playerData;
    private Activity activity;
    private Context context;


    //Layouts

    RelativeLayout outerLayout;
    RelativeLayout playerDetailsLayout;
    RelativeLayout.LayoutParams params;
    ImageView userImage;
    TextView txtVotes;
    RadioButton radioVote;

    public PlayerAdapter(List<PlayerProperties> player,Activity activity,Context context)
    {
        playerData=player;
        this.activity=activity;
        this.context=context;

    }



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

    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(10, 10, 10, 10);
        userImage=new ImageView(activity.getApplicationContext());
        userImage.setId(10000+position);
        userImage.setLayoutParams(params);
        userImage.setBackgroundResource(R.drawable.player);

        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW,userImage.getId());
        playerDetailsLayout=new RelativeLayout(activity.getApplicationContext());
        playerDetailsLayout.setId(11000+position);
        playerDetailsLayout.setBackgroundResource(R.drawable.greybg);
        playerDetailsLayout.setLayoutParams(params);


        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params.addRule(RelativeLayout.BELOW,playerDetailsLayout.getId());
        radioVote=new RadioButton(activity.getApplicationContext());
        radioVote.setId(17000+position);
        radioVote.setLayoutParams(params);






        outerLayout=new RelativeLayout(activity.getApplicationContext());
        outerLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT));


        //outerLayout.addView(userImage);
        outerLayout.addView(userImage);
        outerLayout.addView(playerDetailsLayout);
        outerLayout.addView(radioVote);

        return outerLayout;

    }





} 
包定制适配器;
导入java.util.List;
导入属性Classes.PlayerProperties;
导入android.app.Activity;
导入android.content.Context;
导入android.graphics.Color;
导入android.graphics.Typeface;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AbsListView.LayoutParams;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.RadioButton;
导入android.widget.RadioGroup;
导入android.widget.RelativeLayout;
导入android.widget.TextView;
公共类PlayerAdapter扩展BaseAdapter{
私人名单播放数据;
私人活动;
私人语境;
//布局
相对的外部布局;
相对年轻球员的细节;
RelativeLayout.LayoutParams参数;
ImageView用户图像;
TextView TXT投票;
无线按钮无线投票;
公共播放器适配器(列表播放器、活动、上下文)
{
playerData=玩家;
这个。活动=活动;
this.context=context;
}
@凌驾
public int getCount(){
返回playerData.size();
}
@凌驾
公共对象getItem(int位置){
返回位置;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
params=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,
RelativeLayout.LayoutParams.WRAP_内容);
参数设置边距(10,10,10,10);
userImage=newImageView(activity.getApplicationContext());
userImage.setId(10000+个位置);
userImage.setLayoutParams(params);
setBackgroundResource(R.drawable.player);
params=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,
RelativeLayout.LayoutParams.WRAP_内容);
params.addRule(RelativeLayout.down,userImage.getId());
playerDetailsLayout=新的RelativeLayout(activity.getApplicationContext());
playerDetailsLayout.setId(11000+位置);
playerDetailSlayyout.setBackgroundResource(R.drawable.greybg);
playerDetailsLayout.setLayoutParams(参数);
params=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,
RelativeLayout.LayoutParams.WRAP_内容);
参数addRule(相对中心水平);
params.addRule(RelativeLayout.down,playerDetailsLayout.getId());
radioVote=新的单选按钮(activity.getApplicationContext());
radioVote.setId(17000+位置);
radioVote.setLayoutParams(参数);
outerLayout=新的RelativeLayout(activity.getApplicationContext());
outerLayout.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.WRAP_内容));
//addView(userImage);
addView(userImage);
outerLayout.addView(playerDetailsLayout);
外部布局。添加视图(无线投票);
返回外部布局;
}
} 

您不使用XML文件和重用视图的任何原因?你现在做事情的方式是非常低效的,因为你将不断地创建新的视图,而不是重复使用可以回收的旧视图。您在代码中所做的一切都可以在XML中完成,而XML更干净、更易于管理:mainLayout.addView(grdPlayers、mParams);
package customAdapters;

import java.util.List;



import PropertiesClasses.PlayerProperties;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PlayerAdapter extends BaseAdapter {


    private List<PlayerProperties> playerData;
    private Activity activity;
    private Context context;


    //Layouts

    RelativeLayout outerLayout;
    RelativeLayout playerDetailsLayout;
    RelativeLayout.LayoutParams params;
    ImageView userImage;
    TextView txtVotes;
    RadioButton radioVote;

    public PlayerAdapter(List<PlayerProperties> player,Activity activity,Context context)
    {
        playerData=player;
        this.activity=activity;
        this.context=context;

    }



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

    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(10, 10, 10, 10);
        userImage=new ImageView(activity.getApplicationContext());
        userImage.setId(10000+position);
        userImage.setLayoutParams(params);
        userImage.setBackgroundResource(R.drawable.player);

        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW,userImage.getId());
        playerDetailsLayout=new RelativeLayout(activity.getApplicationContext());
        playerDetailsLayout.setId(11000+position);
        playerDetailsLayout.setBackgroundResource(R.drawable.greybg);
        playerDetailsLayout.setLayoutParams(params);


        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params.addRule(RelativeLayout.BELOW,playerDetailsLayout.getId());
        radioVote=new RadioButton(activity.getApplicationContext());
        radioVote.setId(17000+position);
        radioVote.setLayoutParams(params);






        outerLayout=new RelativeLayout(activity.getApplicationContext());
        outerLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT));


        //outerLayout.addView(userImage);
        outerLayout.addView(userImage);
        outerLayout.addView(playerDetailsLayout);
        outerLayout.addView(radioVote);

        return outerLayout;

    }





}