简体   繁体   English

每行类型2种不同的布局

[英]2 different layouts for the type of each row

I know that there are similar questions, but i couldn't really understand, so i decided to ask again. 我知道也有类似的问题,但是我真的听不懂,所以我决定再问一次。

I am trying to do something similar to a chat, so in the list view there would be 2 types of rows, the ones of the received items and the ones of the sent items. 我正在尝试执行类似于聊天的操作,因此在列表视图中将有2种类型的行,即已接收项目的行和已发送项目的行。 The ones that were received will inflate the recvRow.xml layout, and the ones that were sent will inflate the sentRow.xml layout. 收到的将使recvRow.xml布局膨胀,而已发送的将使sendRow.xml布局膨胀。

I have a list of items that are suposed to be shown ( name, img, etc) and i have a boolean that represents if the object was sent or received. 我有一个应该显示的项目列表(名称,img等),并且我有一个布尔值,表示是否已发送或接收对象。

The problem is that those layouts have different buttons and if for example i receive something first, it will obviously load the recvRow.xml , but then if I try to send something, it will throw a nullpointerexception because on one of the buttons that only exists in the sentRow.xml layout (and doesn't exist in the recvRow.xml). 问题是这些布局具有不同的按钮,例如,如果我先接收到某些内容,则显然会加载recvRow.xml,但是如果我尝试发送某些内容,它将抛出nullpointerexception,因为在仅存在的一个按钮上在sendRow.xml布局中(并且recvRow.xml中不存在)。

Since i am checking that boolean that i told you guys before, i don't see the need to use the getItemViewType method. 因为我正在检查我之前告诉过的布尔值,所以我看不到需要使用getItemViewType方法。

Here is my code for the getView method: 这是我的getView方法代码:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    viewHolder = null;
    if (convertView == null) {

        viewHolder=new ViewHolder();
        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        System.out.println("Transf Rec Adapter : Going to draw AN ITEM : "+ listOfItems.get(position).getAppName());
        System.out.println("Transf Rec Adapter : The received bool is : "+ listOfItems.get(position).isReceived());

        if(listOfItems.get(position).isReceived()){

            view=layoutInflater.inflate(R.layout.highway_transf_record_recv_row,parent,false);
     viewHolder.deleteFile=(ImageView) view.findViewById(R.id.transfRecRowDelete);
     viewHolder.installButton = (ImageView) view.findViewById(R.id.transfRecRowInstall);

        }else{//se enviado
            view=layoutInflater.inflate(R.layout.highway_transf_record_sent_row,parent,false);

        viewHolder.reSendButton=(ImageView) view.findViewById(R.id.transfRecReSendButton);

        }
        viewHolder.appImageIcon=(ImageView) view.findViewById(R.id.transfRecRowImage);
        viewHolder.appNameLabel=(TextView) view.findViewById(R.id.transfRecRowText);
        viewHolder.appVersionLabel = (TextView) view.findViewById(R.id.transfRecRowAppVersion);
        viewHolder.senderInfo = (TextView) view.findViewById(R.id.apkOrigin);
        viewHolder.rowImageLayout = (RelativeLayout) view.findViewById(R.id.transfRecRowImageLayout);
        viewHolder.appInfoLayout = (RelativeLayout) view.findViewById(R.id.appInfoLayout);


    }else{
        view= convertView;
        viewHolder = (ViewHolder) convertView.getTag();
    }



    if(listOfItems.get(position).isReceived()){
        System.out.println("INSIDE THE GET VIEW TRANSF REC ADAPTER :  RECEIVED SOMETHING");
        viewHolder.senderInfo.setText("You received this app :");


        myOnClickListenerToInstall = new MyOnClickListenerToInstall(position);
        viewHolder.installButton.setOnClickListener(myOnClickListenerToInstall);

        myOnClickListenerToDelete= new MyOnClickListenerToDelete(position);
        viewHolder.deleteFile.setOnClickListener(myOnClickListenerToDelete);

    }else{
        System.out.println("INSIDE THE GET VIEW TRANSF REC ADAPTER :  SENT SOMETHING");
        viewHolder.senderInfo.setText("You sent this app :");
        ReSendListener reSendListener=new ReSendListener(position);

        viewHolder.reSendButton.setOnClickListener(reSendListener);//null pointer exception here

    }
    viewHolder.appNameLabel.setText(listOfItems.get(position).getAppName());
    viewHolder.appImageIcon.setImageDrawable(listOfItems.get(position).getIcon());
    viewHolder.appVersionLabel.setText(listOfItems.get(position).getVersionName());

    view.setTag(viewHolder);

    return view;
}

The id's are right and everything exists on the viewHolder. 的ID是正确的,一切都存在于viewHolder。

You need to use getItemViewType because of recycling. 由于回收,您需要使用getItemViewType。 Otherwise you can try to recycle a received item into a non-received view, which will screw things up royally. 否则,您可以尝试将收到的物品回收到未收到的视图中,这将使事情更加混乱。 However it is ok for getItemViewType to just return 0 or 1 based on that boolean. 但是,基于该布尔值,getItemViewType仅返回0或1是可以的。 It doesn't need to be very complicated for 2 view types. 对于两种视图类型,它并不需要很复杂。

You should override getItemViewType() in your adapter. 您应该在适配器中重写getItemViewType() This method will indicate the type of view which should be inflated for each row. 此方法将指示应为每一行放大的视图类型。 You also need to override getViewTypeCount() . 您还需要重写getViewTypeCount()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM