Java泛型 从Activity传递到Fragment再到适配器

huangapple go评论76阅读模式
英文:

Java Generic from Activity to fragment to adapter

问题

我正在开发 Android 应用程序,已经设计了一个通用适配器,参数化适配器将继承该适配器。

public abstract class CarAdapter<T> extends RecyclerView.Adapter<CarAdapter<T>.BaseCarViewHolder> {
    protected DialogMakerCar<T> dialogMaker;
    protected Context context;
    // ...
}

子类:

public class CarItemAdapter extends CarDialogAdapter<Item> {
    public CarItemAdapter(Context context, DialogMakerCar<Item> dialogMaker) {
        super(dialogMaker, context);
    }
}

我在一个片段中调用了这个适配器,而该片段也需要是通用的,以便我可以从活动中调用此片段和适配器以供自定义对象使用。

// 调用适配器的片段,但是它不允许我使用<T>通用类型
adapter = new CarItemAdapter(getContext(), dialogMaker);
gridRecycler.setHasFixedSize(true);
gridRecycler.setLayoutManager(adapter.getLayoutManager());

如果需要更多细节,请告诉我,谢谢。

英文:

I am working in android app, have designed a generic adapter and parameterized adapter will inherit this.

public abstract class CarAdapter&lt;T&gt; extends RecyclerView.Adapter&lt;CarAdapter&lt;T&gt;.BaseCarViewHolder&gt; {
    protected DialogMakerCar&lt;T&gt; dialogMaker;
    protected Context context;
......

child class

public class CarItemAdapter extends CarDialogAdapter&lt;Item&gt; {

    public CarItemAdapter(Context context, DialogMakerCar&lt;Item&gt; dialogMaker) {
        super(dialogMaker, context);
    }

I am calling this adapter in a fragment and that fragment is also need to be generic so that i can call this fragment and adapter for custom objects from activity.

//Fragment to call adapter but its not letting me to use &lt;T&gt; generic 
 adapter = new CarItemAdapter(getContext(),dialogMaker);
        gridRecycler.setHasFixedSize(true);
        gridRecycler.setLayoutManager(adapter.getLayoutManager());

Please let me know if you need more details, Thanks

答案1

得分: 1

以下是翻译好的内容:

我不是 Android 开发者,所以不能给你一个真实的示例。让我们定义如下接口(我不知道你实际需要的方法,所以只是举了一个简单的例子)

public interface ICarAdapter {

    public <T> DialogMakerCar<T> getDialogMaker();

    public Context getContext();

}

你可以定义 T 继承自哪个类,以避免在其他类中使用时出现警告。

现在你需要在你定义的类中使用这个接口:

public class CarItemAdapter extends DialogMakerCar<Item> implements ICarAdapter{
  private DialogMakerCar<Item> dialogMakerCar;
  private Context context;

  public CarItemAdapter(Context cntxt, DialogMakerCar<Item> dialogMaker) {
    dialogMakerCar = dialogMaker;
    context = cntxt;
  }

  /** 这将会给你一个警告,如果在接口中定义 T 继承了 Item 实现的接口,可以避免这个警告。*/
  @Override
  public DialogMakerCar<Item> getDialogMaker() {
    return dialogMakerCar;
  }

  @Override
  public Context getContext() {
    return context;
  }
}

在片段类中:

public class Fragment {
   
  public <T> ICarAdapter createAdapter(T item) {
    // 你可以根据需要进行管理,使用 switch、定义枚举或传递所需的类。
    if (item instanceof Item) {
        return new CarItemAdapter(getContext(),dialogMaker);
    }
    return null;
}

然后当你想要使用它时:

public class Activity {

    Fragment fragment;

    ICarAdapter adapter;

    public Activity() {
        fragment = new Fragment();
        Item item = new Item();
        adapter = fragment.createAdapter(item);
        adapter.getContext();
        adapter.getDialogMaker();
    }
}

希望至少对你有所帮助

其他实现方法

https://dzone.com/articles/java-generic-factory

https://stackoverflow.com/a/47464801/6708879

https://stackoverflow.com/questions/6093363/generic-factory-with-unknown-implementation-classes

<details>
<summary>英文:</summary>

I&#39;m not an Android dev, so can&#39;t really give you a real example.
Lets define the interface as following ( I don&#39;t know the real method you need so just made a simple example)

    public interface ICarAdapter {
    
        public &lt;T&gt; DialogMakerCar&lt;T&gt; getDialogMaker();
    
        public Context getContext();
    
    }

You can defined what the T extends from to avoid warnings when used in the other classes.

Now you need to use this interface in the classes you defined:

    public class CarItemAdapter extends DialogMakerCar&lt;Item&gt; implements ICarAdapter{
      private DialogMakerCar&lt;Item&gt; dialogMakerCar;
      private Context context;

      public CarItemAdapter(Context cntxt, DialogMakerCar&lt;Item&gt; dialogMaker) {
        dialogMakerCar = dialogMaker;
        context = cntxt;
      }

      /** This will give you a warning, can be avoided 
          if you define in the interface that T extendes 
          an interface that Item implements.*/
      @Override
      public DialogMakerCar&lt;Item&gt; getDialogMaker() {
        return dialogMakerCar;
      }

      @Override
      public Context getContext() {
        return context;
      }
    }

In the fragment class:

    public class Fragment {
       
      public &lt;T&gt; ICarAdapter createAdapter(T item) {
        //You can manage this as you want, 
        //with a switch, defining an Enum or passing the Class that you want. 
        if (item instanceof Item) {
            return new CarItemAdapter(getContext(),dialogMaker);
        }
        return null;
    }

An then when you want to use it:

    public class Activity {
    
        Fragment fragment;
    
        ICarAdapter adapter;
    
        public Activity() {
            fragment = new Fragment();
            Item item = new Item();
            adapter = fragment.createAdapter(item);
            adapter.getContext();
            adapter.getDialogMaker();
        }

Hope at least it helps you a bit.

Other ways to do this:

https://dzone.com/articles/java-generic-factory

https://stackoverflow.com/a/47464801/6708879

https://stackoverflow.com/questions/6093363/generic-factory-with-unknown-implementation-classes



</details>



huangapple
  • 本文由 发表于 2020年6月29日 16:08:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/62633753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定