### Initialize ProportionView in Android Activity
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This Java code initializes the `ProportionView` component by finding it by its ID (`R.id.pv`) within an Android Activity. This is the basic step to get a reference to the view after it's defined in XML.
```java
final ProportionView view = (ProportionView) findViewById(R.id.pv);
```
--------------------------------
### Initialize ExplorerView in Android Activity
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This Java code initializes the `FileExplorer` component by finding it by its ID (`R.id.ex`) within an Android Activity. This is the basic step to get a reference to the view after it's defined in XML.
```java
fileExplorer = (FileExplorer)findViewById(R.id.ex);
```
--------------------------------
### Basic Initialization of ReadView for Novel Reading
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This Java code demonstrates the simplest way to initialize and display a `ReadView` component. It creates a new `ReadView` instance, passing the current context and the path to the novel file, then sets it as the content view of the Activity.
```java
ReadView readView = new ReadView(this,dir.getPath());
setContentView(readView);
```
--------------------------------
### Initialize ReadView with File URI from Intent
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This Java code shows how to initialize `ReadView` when an Activity is launched with a file URI via an `Intent`. It retrieves the URI, converts it to a `File` object, and then uses the file's path to instantiate `ReadView`. If no file URI is provided, the Activity finishes.
```java
File dir = null;
Uri fileUri = getIntent().getData();
if (fileUri != null) {
dir = new File(fileUri.getPath());
}
readView = null;
if (dir != null) {
readView = new ReadView(this,dir.getPath());
}
else
finish();
setContentView(readView);
```
--------------------------------
### Implement ExplorerView Item Click and File Chosen Listeners
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This Java code sets up event listeners for the `FileExplorer`. It shows how to override the default long-click behavior to do nothing and how to handle file selection. When a file is chosen, it creates an `Intent` to open `CodeActivity` and passes the selected file's URI.
```java
//覆盖屏蔽原有长按事件
fileExplorer.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> parent, View view, int position, long id) {
return false;
}
});
//选择文件 默认打开CodeView
fileExplorer.setOnFileChosenListener(new OnFileChosenListener() {
@Override
public void onFileChosen(Uri fileUri) {
Intent intent = new Intent(ExplorerActivity.this, CodeActivity.class);
intent.setData(fileUri);
startActivity(intent);
}
});
```
--------------------------------
### Configure ExplorerView Current and Root Directories
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This Java code demonstrates how to programmatically set the current and root directories for the `FileExplorer`. Setting `setCurrentDir` defines the initial path displayed, while `setRootDir` restricts navigation to prevent users from accessing paths beyond a specified root, typically the external storage directory.
```java
// 打开路径
fileExplorer.setCurrentDir(Environment.getExternalStorageDirectory().getPath());
// 根路径(能到达最深的路径,以此避免用户进入root路径)
fileExplorer.setRootDir(Environment.getExternalStorageDirectory().getPath());
```
--------------------------------
### Register ProportionView Path Changed Listener for File Analysis
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This Java code registers an `OnPathChangedListener` with the `FileExplorer`. When the path changes, this listener updates the `ProportionView` by calling `setData` with file proportion data obtained from `fileExplorer.getPropotionText(path)`. It includes error handling for inaccessible paths or empty folders.
```java
//新路径下分析文件比例
fileExplorer.setOnPathChangedListener(new OnPathChangedListener() {
@Override
public void onPathChanged(String path) {
try {
view.setData(fileExplorer.getPropotionText(path));
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "此路径下不可访问或文件夹下无文件", Toast.LENGTH_LONG).show();
}
}
});
```
--------------------------------
### Handle Back Key for ExplorerView Navigation
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This Java method overrides the `onKeyDown` event to manage back button presses within an Activity containing `FileExplorer`. If the back key is pressed, it attempts to navigate to the parent directory in the `FileExplorer`. If navigation is not possible (e.g., at root), it implements a double-tap-to-exit mechanism.
```java
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_DOWN) {
if(!fileExplorer.toParentDir()){
if(System.currentTimeMillis() - exitTime < 1000)
finish();
exitTime = System.currentTimeMillis();
Toast.makeText(this, "再次点击退出", Toast.LENGTH_SHORT).show();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
```
--------------------------------
### Integrate ExplorerView in XML Layout
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This XML snippet demonstrates how to add the `FileExplorer` component to an Android layout. It defines the `FileExplorer` with `match_parent` for both width and height, and assigns it the ID `@+id/ex` for programmatic access.
```xml
```
--------------------------------
### Integrate ProportionView in XML Layout
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This XML snippet shows how to add the `ProportionView` component to an Android layout. It sets the width to `match_parent`, a fixed height of `30dp`, and adds a 10dp margin around the view. It's assigned the ID `@+id/pv` for programmatic access.
```xml
```
--------------------------------
### Handle Options Menu Item for CodeView Editing
Source: https://github.com/lfkdsk/justwetools/blob/master/README.md
This Java method handles the selection of an options menu item. Specifically, it toggles the editability of a `codeView` component. If the `action_code` item is selected, it switches between 'Edit' and 'Complete' states, updating the menu item title and the `codeView`'s editable status accordingly.
```java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (item.getItemId() == R.id.action_code) {
if (!codeView.isEditable()) {
item.setTitle("完成");
codeView.setContentEditable(true);
} else {
item.setTitle("编辑");
codeView.setContentEditable(false);
}
}
return super.onOptionsItemSelected(item);
}
```
--------------------------------
### JavaScript: Markdown Parsing and Scroll Alignment
Source: https://github.com/lfkdsk/justwetools/blob/master/justwetools/src/main/assets/markdown.html
This snippet defines two JavaScript functions: `parseMarkdown` and `alignScroll`. `parseMarkdown` uses the 'marked' library to convert Markdown content into HTML, setting various parsing options (e.g., GFM, tables, no breaks, sanitize). It then injects the resulting HTML into an element with the ID 'content' and calls `alignScroll`. `alignScroll` is a callback function that notifies a global 'handler' object when HTML generation is complete, likely for UI synchronization or post-rendering adjustments.
```javascript
function parseMarkdown(content) { marked.setOptions({ renderer: new marked.Renderer(), gfm: true, tables: true, breaks: false, pedantic: false, sanitize: true, smartLists: true, smartypants: false }); document.getElementById('content').innerHTML = marked(content); alignScroll(); } function alignScroll() { window.handler.onHTMLGenerated(); }
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.