6

Tôi có chế độ xem danh sách với cursoradapter. Bây giờ tôi muốn triển khai quảng cáo gốc express trong chế độ xem danh sách.Cách triển khai Quảng cáo gốc express trong Chế độ xem danh sách với CursorAdapter

Tôi đã thấy việc triển khai Quảng cáo gốc với baseAdapter đơn giản, nói chung chúng tôi đang sử dụng List<Object> để chuyển dữ liệu sang bộ điều hợp và kiểm tra loại mục bên trong phương thức getView() để thêm quảng cáo.

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
     throws IllegalArgumentException { 
    Object item = getItem(position); 

    if (item instanceof Listing) { 
     // Listing items already have all the data required, so they just need to be displayed. 
        return listingLayout; 
    } else if (item instanceof AdPlacement) { 
     return ((AdPlacement) item).getView(convertView, parent); 
    } else { 
     // Any unknown items will cause exceptions, though this shouldn't ever happen. 
     throw new IllegalArgumentException(
       String.format("Adapter can't handle getView() for list item of type %s", 
         item.getClass().getName())); 
    } 


} 

Làm thế nào để kiểm tra điều kiện này trong CursorAdapter như CursorAdapter chỉ có phương pháp newItem() với các chi tiết con trỏ

@Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View v = LayoutInflater.from(context).inflate(R.layout.list_item_station, parent, false); 
     ViewHolder holder = new ViewHolder(); 

     v.setTag(holder); 
     return v; 
    } 

Làm thế nào để thêm quảng cáo có nguồn gốc sau mỗi 10 mục trong CursorAdapter

Bellow là mã hiện tại tôi đang sử dụng để thêm dữ liệu trong danh sách.

public class StationsCursorAdapter extends CursorAdapter{ 


    public StationsCursorAdapter(Context context) { 
     super(context, null, true); 
      } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View v = LayoutInflater.from(context).inflate(R.layout.list_item_station, parent, false); 
     ViewHolder holder = new ViewHolder(); 

     v.setTag(holder); 
     return v; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     ViewHolder holder = (ViewHolder) view.getTag(); 
     holder.titleTextView.setText(cursor.getString(cursor.getColumnIndex(Station.NAME))); 
    } 

    private static final class ViewHolder { 
       TextView titleTextView; 
      } 

} 

Trả lời

1

Chế độ xem triển khai Mục loại khác nhau. Bạn nên xác định các loại bố cục khác nhau và tăng cường. Hãy xem RecyclerView Item Type.

Sau khi bạn tạo Chế độ xem xem chính xác, bạn có thể kiểm tra loại trong phương thức OnBind và triển khai một hành vi khác cho từng Chế độ xem.

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    ViewHolder holder = (ViewHolder) view.getTag(); 
    if (holder instance of Dog) { 
     // Handle one case 
    } else { 
     // Handle other case 
    } 
} 
+0

Nhưng làm cách nào tôi có thể đặt viewType khi chúng tôi đang sử dụng bộ điều hợp curson? –

+0

@OmInfowaveDevelopers Về cơ bản, bạn tạo một số ViewHolder. Mỗi để thực hiện của bạn. ViewHolderDog, ViewHolderCat. Trong 'NewView' bạn sẽ tìm nạp một số thông tin về kiểu từ Cursor, và sau đó trả về ViewHolder Type đặc biệt. Sau đó, trong 'OnBind', bạn sẽ nhận được ViewHolder, và bạn sẽ kiểm tra loại đó là gì. – GensaGames

+0

, Nhưng vấn đề là CursorAdpater nó trực tiếp truyền con trỏ đến adapter, giả sử có 22 mục trong cơ sở dữ liệu cục bộ sau đó kích thước con trỏ là 22 thì kích thước bộ điều hợp cũng là 22, bây giờ tôi muốn thêm quảng cáo sau mỗi 10 mục để kích thước bộ điều hợp phải 24. Làm thế nào để thêm hai mục bổ sung này trong bộ điều hợp con trỏ vì chúng tôi không thể sửa đổi giá trị Con trỏ –

1

tạo tệp list_item_native_ad.xml.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="132dp"> 

    <com.google.android.gms.ads.NativeExpressAdView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/nativeAd" 
     ads:adSize="FULL_WIDTHx132" 
     ads:adUnitId="@string/native_ad_unit_id"/> 
</LinearLayout> 

Tạo NativeAdViewHelper bên trong bộ điều hợp của bạn.

public class NativeAdViewHolder extends RecyclerView.ViewHolder { 

     private final NativeExpressAdView mNativeAd; 

     public NativeAdViewHolder(View itemView) { 
      super(itemView); 
      mNativeAd = (NativeExpressAdView) itemView.findViewById(R.id.nativeAd); 
      mNativeAd.setAdListener(new AdListener() { 
       @Override 
       public void onAdLoaded() { 
        super.onAdLoaded(); 
        if (mItemClickListener != null) { 
         Log.i(TAG, "onAdLoaded"); 
        } 
       } 

       @Override 
       public void onAdClosed() { 
        super.onAdClosed(); 
        if (mItemClickListener != null) { 
         Log.i(TAG, "onAdClosed"); 
        } 
       } 

       @Override 
       public void onAdFailedToLoad(int errorCode) { 
        super.onAdFailedToLoad(errorCode); 
        if (mItemClickListener != null) { 
         Log.i(TAG, "onAdFailedToLoad"); 
        } 
       } 

       @Override 
       public void onAdLeftApplication() { 
        super.onAdLeftApplication(); 
        if (mItemClickListener != null) { 
         Log.i(TAG, "onAdLeftApplication"); 
        } 
       } 

       @Override 
       public void onAdOpened() { 
        super.onAdOpened(); 
        if (mItemClickListener != null) { 
         Log.i(TAG, "onAdOpened"); 
        } 
       } 
      }); 
      AdRequest adRequest = new AdRequest.Builder() 
        .addTestDevice(MainActivity.TEST_DEVICE_ID) 
        .build(); 
      //You can add the following code if you are testing in an emulator 
      /*AdRequest adRequest = new AdRequest.Builder() 
       .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
       .build();*/ 
      mNativeAd.loadAd(adRequest); 
     } 
    } 

phương pháp ghi đè getItemViewType() bên trong bộ chuyển đổi của bạn

@Override 
    public int getItemViewType(int position) { 
     if (position>1 && position % 3 == 0) {//in your case replace % 3 with % 10. 
      return NATIVE_AD_VIEW_TYPE; 
     } 
     return DEFAULT_VIEW_TYPE; 
    } 

phương pháp ghi đè getViewTypeCount()

@Override 
public int getViewTypeCount() { 
      return 2; 
     } 

bên trong phương pháp newView() bạn

@Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View view; 
     LayoutInflater layoutInflater = LayoutInflater.from(mContext); 
     switch (viewType) { 
      default: 
       view = layoutInflater 
         .inflate(R.id.content, parent, false); 
       return new ViewHolder(view); 
      case NATIVE_AD_VIEW_TYPE: 
       view = layoutInflater.inflate(R.layout.list_item_native_ad, parent, false); 
       return new NativeAdViewHolder(view); 
     } 
    } 

bên trong phương thức bindView()

@Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     if (!(holder instanceof ViewHolder)) { 
      return; 
     } 
     holder.titleTextView.setText(cursor.getString(cursor.getColumnIndex(Station.NAME))); 
    } 

Hy vọng nó sẽ giúp bạn!

+0

cảm ơn bạn đã trả lời. Vấn đề là giả sử có 15 mục trong con trỏ, sau đó nó gọi phương thức newView() trong 15 lần chỉ cách gọi phương thức này thêm một lần để xem lại quảng cáo từ phương thức newView(). –

+0

@OmInfowaveDevelopers https://stackoverflow.com/a/40608363/392506 –

+0

@OmInfowaveNhà phát triển bạn có thể vui lòng chấp nhận câu trả lời nếu nó hoạt động cho bạn –

1

Tôi khuyên bạn không nên sử dụng bộ điều hợp con trỏ cho trường hợp này, vì nó sẽ không hiệu quả, thay vào đó bạn nên lấy dữ liệu từ Cơ sở dữ liệu vào danh sách và sử dụng BaseAdapter mở rộng CustomAdapter để ràng buộc các khung nhìn tương ứng.

Và nếu bạn chỉ sử dụng bộ điều hợp con trỏ, thì bạn cần sửa đổi dữ liệu con trỏ để có mục quảng cáo ở mọi vị trí thứ 10. Bạn có thể thay đổi con trỏ của mình như:

public Cursor getModifiedCursorWithAds(Cursor cursor) { 
     int size = cursor.getCount(); 
     MatrixCursor matrixCursor = new MatrixCursor(cursor.getColumnNames()); 
     int totalAds = size/10; 
     int negativeIds = -1; 
     cursor.moveToFirst(); 
     //assuming only two columns in cursor 
     for (int i = 0; i < (size + totalAds); i++) { 
      if (i % 10 == 0 && i != 0) { 
       matrixCursor.addRow(new String[]{"" + negativeIds--, "Ads data"}); //add dummy data for displaying ads(on the basis of negative ids, ad will be displayed 
      } else { 
       matrixCursor.addRow(new String[]{cursor.getString(0), cursor.getString(1)}); //add data from actual cursor, if you have more than 2 data modify this statement 
       cursor.moveToNext(); 
      } 
     } 
     cursor.close(); 
     return matrixCursor; 
    } 

Sửa đổi bạn newView để trả lại chế độ xem khác nhau cho quảng cáo và các mục khác và ràng buộc theo ràng buộcXem lại gọi lại.

@Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     View view; 
     LayoutInflater layoutInflater = LayoutInflater.from(mContext); 
     int viewType = cursor.getInt(0); 
     if (viewType >= 0) { 
      view = layoutInflater 
        .inflate(R.id.list_item_content, parent, false); 
      ViewHolder holder = new ViewHolder(); 
      view.setTag(holder); 
     } else { 
      view = layoutInflater.inflate(R.layout.list_item_native_ad, parent, false); 
      AdViewHolder holder = new AdViewHolder(); 

      view.setTag(holder); 
     } 
     return view; 
    } 

Tôi chưa thử nghiệm mã trên, vì vậy có thể cần một số thay đổi để làm cho nó hoạt động.

+0

cảm ơn bạn đã trả lời một cách hợp lý nó trông đúng! những gì về hiệu suất của ứng dụng như tôi có khoảng 1000 mục trong danh sách? –

+0

Không hiệu quả lắm, vì bạn phải duyệt toàn bộ dữ liệu của con trỏ một lần. Độ phức tạp thời gian của getModifiedCursorWithAds là O (n) trong đó n là kích thước của dữ liệu con trỏ. – abhishesh

Các vấn đề liên quan