ChannelView

repository·master·Indexed 43 days ago

https://github.com/chengzhicao/channelview

An Android component for creating customizable channel selectors that support dragging, reordering, adding, and deleting channels. Version v1.1.2 introduces an Adapter pattern using StyleAdapter, BaseStyleAdapter, and DefaultStyleAdapter for data population and custom styling, replacing the legacy inflateData approach.

Tokens
3.7K
Snippets
8
Records
9
Agent score
6%

What's inside ChannelView

  1. How to customize ChannelView using Adapters

    master

    Since version 1.1.0, the recommended way to populate data and customize styles is using the Adapter pattern. The older inflateData() method and direct property setters are deprecated.

    Adapter Hierarchy

    • StyleAdapter (Interface): The base interface defining data population and style implementation.
    • BaseStyleAdapter (Abstract Class): Implements some StyleAdapter methods; use this to avoid implementing every single method when creating custom styles.
    • DefaultStyleAdapter (Class): Extends BaseStyleAdapter and provides the default implementation. Use this if you only want to override specific parts of the default look.

    Implementation Workflow

    1. Create a class extending BaseStyleAdapter (or DefaultStyleAdapter).
    2. Implement getChannelData() to return your LinkedHashMap<String, List<Channel>>.
    3. Override style methods like setNormalStyle, setFixedStyle, setEditStyle, or setFocusedStyle to apply custom layouts/colors via a ViewHolder.
    4. Register the adapter using channelView.setStyleAdapter(yourAdapter).
    // Example of extending BaseStyleAdapter
    class MyAdapter extends BaseStyleAdapter<MyAdapter.MyViewHolder> {
        @Override
        public MyViewHolder createStyleView(ViewGroup parent, String channelName) {
            // Inflate your custom layout here
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_custom_channel, null);
            MyViewHolder customViewHolder = new MyViewHolder(inflate);
            customViewHolder.tv.setText(channelName);
            return customViewHolder;
        }
    
        @Override
        public LinkedHashMap<String, List<Channel>> getChannelData() {
            return data;
        }
    
        @Override
        public void setNormalStyle(MyViewHolder viewHolder) {
            viewHolder.tv.setBackgroundResource(R.drawable.bg_channel_custom_normal);
        }
        // ... other style overrides
    }
  2. Use ChannelView in your Layout

    master

    Add the ChannelView component to your XML layout file. Ensure you use the full package name.

    <com.cheng.channelview.ChannelView
        android:id="@+id/channelView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  3. Install ChannelView via JitPack

    master

    To use ChannelView in your Android project, add the JitPack repository to your root build.gradle file and then add the dependency to your app-level build.gradle file.

    Note: It is highly recommended to set your compileSdkVersion to 28 or higher to avoid compilation issues.

    // In root build.gradle
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
    
    // In app build.gradle
    dependencies {
        implementation 'com.github.chengzhicao:ChannelView:v1.1.2'
    }
  4. Implement ChannelView using the legacy approach (Example 2)

    master

    This method involves manually creating Channel objects, organizing them into lists, and adding them to the ChannelView via addPlate.

    Note: This approach is no longer recommended.

    Steps:

    1. Layout: Add the <com.cheng.channelview.ChannelView> component to your XML layout.
    2. Initialization:
      • Create Channel objects. You can specify a channelBelong (plate index) and an insertion position using the Channel(name, belong, position) constructor.
      • Use channelView.setChannelFixedCount(int) to set the number of fixed channels.
      • Use channelView.addPlate(String title, List<Channel> channelList) to add a new category/plate of channels.
      • Call channelView.inflateData() to render the data.
    3. Listeners:
      • Implement ChannelView.OnChannelListener to handle clicks and edit events.
      • Register the listener using channelView.setOnChannelItemClickListener(this).

    Listener Methods:

    • channelItemClick(int position, Channel channel): Triggered when a channel is clicked.
    • channelEditStart(): Triggered when the editing mode begins.
    • channelEditFinish(List<Channel> channelList): Triggered when editing is completed. Returns the updated list of channels.
    // In XML
    <com.cheng.channelview.ChannelView
        android:id="@+id/channelView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    // In Activity
    public class ChannelViewActivity extends AppCompatActivity implements ChannelView.OnChannelListener {
        private ChannelView channelView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_channel_view);
            channelView = findViewById(R.id.channelView);
            init();
        }
    
        private void init() {
            List<Channel> myChannelList = new ArrayList<>();
            // Example of creating a channel with specific belonging and position
            myChannelList.add(new Channel("News", 2, 0));
            myChannelList.add(new Channel("Default"));
    
            channelView.setChannelFixedCount(3);
            channelView.addPlate("My Channels", myChannelList);
            channelView.inflateData();
            channelView.setOnChannelItemClickListener(this);
        }
    
        @Override
        public void channelItemClick(int position, Channel channel) {
            Log.i("TAG", position + ".." + channel);
        }
    
        @Override
        public void channelEditFinish(List<Channel> channelList) {
            Log.i("TAG", channelList.toString());
        }
    
        @Override
        public void channelEditStart() {}
    }
  5. ChannelView Public API Reference

    master

    The following methods are available on the ChannelView instance to control its behavior and retrieve state.

    // Set the adapter for data and styling
    void setStyleAdapter(StyleAdapter styleAdapter)
    
    // Set the position where 'My Channels' should be inserted (defaults to end)
    void setInsertRecommendPosition(int recommendPosition)
    
    // Set the number of fixed channels
    void setChannelFixedCount(int channelFixedCount)
    
    // Check if the channel sequence has changed
    boolean isChange()
    
    // Get the list of 'My Channels'
    List<Channel> getMyChannel()
    
    // Get the list of other channel groups
    List<List<Channel>> getOtherChannel()
    
    // Set the listener for channel events
    void setOnChannelListener(OnChannelListener onChannelListener)
  6. ChannelView Listener Interface Reference

    master

    Use ChannelListenerAdapter (an abstract implementation of OnChannelListener2) to handle user interactions within the ChannelView.

    // Methods available in the listener
    void channelItemClick(int position, Channel channel)
    void channelEditFinish(List<Channel> channelList)
    void channelEditStart()
    void channelEditStateItemClick(int position, Channel channel)
  7. ChannelView Version History and Feature Log

    master

    A summary of features and fixes across versions:

    • v1.1.1: Fixed inaccuracies in channelHorizontalSpacing and channelVerticalSpacing.
    • v1.1.0:
      • Optimized animations.
      • Added channel click event listening during editing mode.
      • Added support for specifying insertion position when adding channels (defaults to end).
      • Updated custom styling and data inflation methods.
    • v1.0.9: Disabled multi-touch and fixed a bug where channels would continuously swap positions during adjustment.
    • v1.0.8: Optimized channel deletion.
    • v1.0.7: Added methods to retrieve other channel content and 3 types of attr properties.
    • v1.0.6:
      • Support for multiple channel plates.
      • Simplified channel setting methods.
      • Two ways to edit: long-press or active button click.
      • Support for setting extra channel attributes (e.g., for saving IDs).
      • Simplified fixed channel settings and custom styling.
  8. ChannelView Property Reference

    master

    These properties can be used to configure the visual appearance of the ChannelView (primarily when using the older inflateData or DefaultStyleAdapter methods).

    channelHeight: Channel height
    channelColumn: Number of columns
    channelPadding: Padding inside channels
    channelVerticalSpacing: Vertical spacing between channels
    channelHorizontalSpacing: Horizontal spacing between channels
    channelFixedCount: Number of fixed channels
    tipEditBackground: Edit button background
    tipEditTextColor: Edit button text color
    tipEditTextSize: Edit button font size
    tipFinishBackground: Finish button background
    tipFinishTextColor: Finish button text color
    tipFinishTextSize: Finish button font size
    platesTitleBold: Whether channel section titles are bold
    platesTitleBackground: Channel section title background
    platesTitleColor: Channel section title color
    platesTitleSize: Channel section title size
    platesTitleHeight: Channel section title height
    platesTitleLeftRightPadding: Channel section title horizontal padding
    otherSubTitleBackground: Background for other channel section subtitles
    otherSubTitleTextColor: Text color for other channel section subtitles
    otherSubTitleTextSize: Font size for other channel section subtitles
    otherSubTitleName: Subtitle name for unselected channels
    subTitleBackground: Background for selected channel section subtitles
    subTitleTextColor: Text color for selected channel section subtitles
    subTitleTextSize: Font size for selected channel section subtitles
    subTitleName: Subtitle name for selected channels
  9. Configure ChannelView attributes via ChannelAttr

    master

    The ChannelAttr class is used to define the properties and types of views within a ChannelView. It encapsulates the view type, its spatial coordinates, its position within channel groups, and the associated channel entity.

    Attribute Types

    Use the following constants to identify the type of view being processed:

    • TITLE (0x01): Represents a title view.
    • CHANNEL (0x02): Represents a channel view.

    Properties

    • type: The integer type of the view (e.g., TITLE or CHANNEL).
    • coordinate: A PointF object representing the (x, y) coordinates of the view.
    • groupIndex: The index of the channelGroups where the view is located.
    • channel: The Channel entity associated with the view.
    // Example of how ChannelAttr properties are structured
    ChannelAttr attr = new ChannelAttr();
    attr.type = ChannelAttr.CHANNEL;
    attr.coordinate = new PointF(100f, 200f);
    attr.groupIndex = 0;
    attr.channel = myChannelInstance;